iOS动画iOS动画动效控件封装

简单封装一个下载进度圆环

2016-01-08  本文已影响411人  静花寒

自己写的,效果如下:

30AC5594-8CCA-413A-9F65-FD1DF23A9E96.png
#import <UIKit/UIKit.h>

@protocol ProgressViewDelegate;

@interface ProgressView : UIView

@property (nonatomic,retain) NSURLRequest *request;
@property (nonatomic,retain) NSURLConnection *connection;
@property (nonatomic,retain) NSMutableData *recvivedData;
@property (nonatomic,retain) NSString *fileName;
@property (nonatomic,assign) id<ProgressViewDelegate> delegate;
@property (nonatomic,retain) UILabel *downloadMB;
@property (nonatomic,retain) UILabel *downloadpercent;
@property (nonatomic,assign) CGFloat currentLength;
@property (nonatomic,assign) CGFloat totalLength;
@property (nonatomic,assign) CGFloat number;
@property (nonatomic,assign) CGFloat radius;

- (ProgressView *)initWithURL:(NSURL *)fileURL progressViewWithFrame:(CGRect)frame font:(UIFont *)font color:(UIColor *)color timeout:(CGFloat)timeout alive:(BOOL)alive radius:(CGFloat)radius delegate:(id<ProgressViewDelegate>)theDlegate;

@end

@protocol ProgressViewDelegate <NSObject>
//下载错误
- (void)progressView:(ProgressView *)progressView didFileWithError:(NSError *)error;

//下载完成

- (void)progressView:(ProgressView *)progressView didFinishedWithData:(NSData *)data suggestedWithFileName:(NSString *)fileName;

//下载中

- (void)progressViewUpdated:(ProgressView *)progressView;

@end


#import "ProgressView.h"
#import <QuartzCore/QuartzCore.h>
#define kWidth [[UIScreen mainScreen] bounds].size.width
#define kHeight [[UIScreen mainScreen] bounds].size.height

@implementation ProgressView

- (void)drawRect:(CGRect)rect
{
    [self drawProgressView2];
    [self drawProgressView];
}

- (void)drawProgressView
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, self.radius / 10.0);
    CGContextSetLineCap(context, kCGLineCapButt);
    CGFloat length[] = {4,8};
    CGContextSetLineDash(context, 0, length, 2);
    [[UIColor orangeColor]set];
    CGFloat end = - 1 * M_PI + (2 * M_PI * self.number);
    CGContextAddArc(context, kWidth / 2, kWidth / 2, self.radius, - 1 * M_PI, end, 0);
    CGContextStrokePath(context);
}

- (void)drawProgressView2
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, self.radius / 10.0);
    CGContextSetLineCap(context, kCGLineCapButt);
    CGFloat length[] = {4,8};
    CGContextSetLineDash(context, 0, length, 2);
    [[UIColor lightGrayColor]set];
    CGFloat end = - 1 * M_PI + (2 * M_PI);
    CGContextAddArc(context, kWidth / 2, kWidth / 2, self.radius, - 1 * M_PI, end, 0);
    CGContextStrokePath(context);
}

- (ProgressView *)initWithURL:(NSURL *)fileURL progressViewWithFrame:(CGRect)frame font:(UIFont *)font color:(UIColor *)color timeout:(CGFloat)timeout alive:(BOOL)alive radius:(CGFloat)radius delegate:(id<ProgressViewDelegate>)theDlegate
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.downloadMB = [self labelWithFrame:(CGRectMake(kWidth / 2 - 70, kWidth / 2 + radius + radius / 10 + 5, 140, 40)) font:font color:color];
        self.downloadpercent = [self labelWithFrame:(CGRectMake(kWidth / 2 - 70, kWidth / 2 + radius + radius / 10 + 50, 140, 40)) font:font color:color];
        if (alive) {
            [self addSubview:self.downloadMB];
            [self addSubview:self.downloadpercent];
        }else{
            
        }
        
        self.delegate = theDlegate;
        self.number = 0.0;
        self.recvivedData = [[NSMutableData alloc] initWithLength:0];
        self.fileName = [[[fileURL absoluteString]lastPathComponent]copy];
        self.radius = radius;
        self.request = [[NSURLRequest alloc] initWithURL:fileURL cachePolicy:(NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:timeout];
        self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:YES];
        if (self.connection == nil) {
            [self.delegate progressView:self didFileWithError:[NSError errorWithDomain:@"域错误" code:1 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"链接不存在",NSLocalizedDescriptionKey, nil]]];
        }
     }
    return self;
    
}

- (UILabel *)labelWithFrame:(CGRect)frame font:(UIFont *)font color:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = color;
    label.font = font;
    return label;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.recvivedData appendData:data];
    self.currentLength += data.length;
    //MB
    self.downloadMB.text = [NSString stringWithFormat:@"%.02fMB/%.02fMB",self.currentLength / 1024.0 / 1024.0,self.totalLength / 1024.0 / 1024.0];
    //百分比
    self.downloadpercent.text = [NSString stringWithFormat:@"%.02f%%",(self.currentLength / self.totalLength) * 100];
    if (self.totalLength != NSURLResponseUnknownLength) {
        self.number = self.currentLength / self.totalLength;
    }
    [self setNeedsDisplay];
    [self.delegate progressViewUpdated:self];
    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.totalLength = [response expectedContentLength];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self.delegate progressView:self didFileWithError:error];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self.delegate progressView:self didFinishedWithData:self.recvivedData suggestedWithFileName:self.fileName];
    NSLog(@"Finish");
    NSString*filePath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0]stringByAppendingPathComponent:self.fileName];
    NSData *data = self.recvivedData;
    [data writeToFile:filePath atomically:NO];//将数据写入Documents目录。
    NSLog(@"%@",filePath);
}

@end

喜欢的点个关注吧,转载请注明出处

上一篇下一篇

猜你喜欢

热点阅读