用CALayer实现下载进度条控件

用CALayer实现下载进度条

效果:

源码:

//
//  ViewController.m
//  ProgressView
//
//  Created by YouXianMing on 14/11/18.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) CALayer *layer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    // CALayer
    _layer                 = [CALayer layer];
    _layer.frame           = CGRectMake(50, 50, 200, 2);
    _layer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:_layer];
    
    // 定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.f
                                              target:self
                                            selector:@selector(timerEvent)
                                            userInfo:nil
                                             repeats:YES];
}

/**
 *  定时器
 */
- (void)timerEvent {
    // 显式动画
    _layer.frame = CGRectMake(50, 50, arc4random()%200, 2);
}

@end

封装后的源码:

//
//  ProgressView.h
//  ProgressView
//
//  Created by YouXianMing on 14/11/18.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ProgressView : UIView

@property (nonatomic, strong) UIColor *progressColor;
@property (nonatomic, assign) CGFloat  progress;

@end
//
//  ProgressView.m
//  ProgressView
//
//  Created by YouXianMing on 14/11/18.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ProgressView.h"

@interface ProgressView ()

{
    CALayer   *_backLayer;
    CGFloat    _frameWidth;
    CGFloat    _layerWidth;
}

@end

@implementation ProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _backLayer       = [CALayer layer];

        _frameWidth      = self.bounds.size.width;
        _layerWidth      = 0;
        
        _backLayer.frame = CGRectMake(0, 0, _layerWidth, frame.size.height);
        [self.layer addSublayer:_backLayer];
    }
    return self;
}

@synthesize progressColor = _progressColor;
- (void)setProgressColor:(UIColor *)progressColor {
    _progressColor             = progressColor;
    _backLayer.backgroundColor = _progressColor.CGColor;
}
- (UIColor *)progressColor {
    return _progressColor;
}

@synthesize progress = _progress;
- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    
    CGRect rect = _backLayer.frame;
    if (_progress <= 0) {
        rect.size.width = 0;
    } else if (_progress >= 1) {
        rect.size.width = _frameWidth;
    } else {
        rect.size.width = _progress * _frameWidth;
    }
    
    _backLayer.frame = rect;
}
- (CGFloat)progress {
    return _progress;
}

@end

 

转载于:https://www.cnblogs.com/YouXianMing/p/4107001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值