ios下载进度条

#import 

@interface WNGDownloadButton : UIView

/** 下载类型*/
@property (nonatomic ,assign)WNGDownloadButtonDownloadType downloadType;

/** 进度*/
@property (nonatomic ,assign)CGFloat progress;

@end


#import "WNGDownloadButton.h"

@interface WNGDownloadButton ()

/** 图片*/
@property (nonatomic ,strong)UIImageView *imageView;

@end

@implementation WNGDownloadButton

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.imageView];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = self.bounds;
}

- (void)setDownloadType:(WNGDownloadButtonDownloadType)downloadType
{
    _downloadType = downloadType;
    NSString *imageName = @"";
    switch (downloadType)
    {
        case WNGDownloadButtonDownloadTypeNomal:
            imageName = @"groping_downLoad";
            
            break;
            
        case WNGDownloadButtonDownloadTypeDownloading:
            imageName = @"downloading";
            
            break;
        
        case WNGDownloadButtonDownloadTypeSuspend:
            imageName = @"groping_downLoad";
            
            break;
            
        case WNGDownloadButtonDownloadTypeFinished:
            imageName = @"rightArrow";
            break;
        default:
            
            break;
    }
    UIImage *image = [UIImage imageNamed:imageName];
    self.imageView.image = image;
}

- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
//    NSLog(@"NSStringFromCGRect%@",NSStringFromCGRect(rect));
    CGFloat radius = WidthScale(rect.size.height);
    CGPoint center = CGPointMake(radius, radius);
    CGFloat startAngle = -M_PI_2;
    CGFloat endAngle = -M_PI_2 + self.progress * 2 * M_PI;
    
    CGFloat adjustR = 32 * Scale;
    
    if (WIDTH > 375)
    {
        adjustR = 37 * Scale;
        center = CGPointMake(radius, radius - 2.5);
    }else if (WIDTH < 375)
    {
        adjustR = 27 * Scale;
        center = CGPointMake(radius, radius + 3.3);
    }
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius - adjustR startAngle:startAngle endAngle:endAngle clockwise:YES];
    path.lineWidth = 2;
    [WNGColor(C_3096FF) set];
    [path stroke];
}

- (UIImageView *)imageView
{
    if (!_imageView)
    {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeCenter;
    }
    return _imageView;
}

- (void)dealloc
{
    self.imageView = nil;
}

@end


你可能感兴趣的:(ios下载进度条)