您的位置:首页 > 其它

CALayer下载进度条

2016-03-13 22:29 239 查看
@interface ProgressView ()
@property (nonatomic,
strong) CALayer *progressLayer;
@property (nonatomic,
assign) CGFloat currentViewWidth;
@end

@implementation ProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super
initWithFrame:frame];
    if (self) {
        self.progressLayer = [CALayer
layer];
        self.progressLayer.frame =
CGRectMake(0,
0, 0, frame.size.height);
        self.progressLayer.backgroundColor = [UIColor
redColor].CGColor;
        [self.layer
addSublayer:self.progressLayer];
        self.currentViewWidth = frame.size.width;
    }
    return
self;
}

#pragma mark - 重写setter getter
方法
@synthesize progress =
_progress;
- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    
    if (progress <=
0) {
        self.progressLayer.frame =
CGRectMake(0,
0, 0,
self.frame.size.height);
    } else
if (progress <= 1) {
        self.progressLayer.frame =
CGRectMake(0,
0, progress * self.currentViewWidth,
self.frame.size.height);
    } else {
        self.progressLayer.frame =
CGRectMake(0,
0, self.currentViewWidth,
self.frame.size.height);
    }
}

- (CGFloat)progress {
    return
_progress;
}

@synthesize layerColor =
_layerColor;
- (void)setLayerColor:(UIColor *)layerColor {

    _layerColor = layerColor;
    self.progressLayer.backgroundColor = layerColor.CGColor;

}

- (UIColor *)layerColor {
    return
_layerColor;
}

VC:

@interface ViewController ()

//@property (nonatomic, strong) CALayer *layer; //单独创建的layer
@property (nonatomic,
strong) ProgressView *progressView;
@property (nonatomic,
strong) NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super
viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 3)];
//    containerView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:containerView];
    
    // 修改该容器的view相关值。
    // containerView.layer.frame = CGRectMake(0, 0, 200, 200);
    // containerView.layer.backgroundColor = [UIColor greenColor].CGColor;
    
    
//    self.layer = [CALayer layer];
//    self.layer.frame = containerView.bounds;
//    self.layer.frame = CGRectMake(0, 0, 0, 3);
//
//    self.layer.backgroundColor = [UIColor greenColor].CGColor;
//    
//    [containerView.layer addSublayer:self.layer];
//    
//    [self performSelectorOnMainThread:@selector(layerAnimation) withObject:nil waitUntilDone:3.f];
    
    self.progressView = [[ProgressView
alloc] initWithFrame:CGRectMake(20,
20, 290,
3)];
    self.progressView.layer.borderWidth
= 1.f;
    self.progressView.layerColor = [UIColor
yellowColor];
    [self.view
addSubview:self.progressView];
    
    [self
performSelectorOnMainThread:@selector(layerAnimation)
withObject:nil
waitUntilDone:3.f];
    
    self.timer = [NSTimer
scheduledTimerWithTimeInterval:1
target:self
selector:@selector(layerAnimation)
userInfo:nil
repeats:YES];
    
}

- (void)layerAnimation {
   // self.layer.frame = CGRectMake(0, 0, 100, 3);
    //self.layer.backgroundColor = [UIColor blackColor].CGColor;

   // self.progressView.progress = 0.5;
    
    self.progressView.progress =
arc4random() % 100 /
100.f;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: