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;
}
@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;
}
相关文章推荐
- Java基础复习String对象创建的分析
- HTML中input和img标签元素同行水平对齐
- 1030. Travel Plan (30)
- Car race game 树状数组
- UVA10020用最少的线段去覆盖给定的线段
- 引用路径 ../ ./ 说明
- BOM
- 【机器学习详解】线性回归、梯度下降、最小二乘的几何和概率解释
- swift 之xib自定义view可视化到storyboard
- 123
- UI自定义进度条控件
- 从start_kernel到init
- Java微信支付之公众号支付、扫码支付
- 文件存储格式转换(ASCII <-> UTF-8)
- Windows静态库和动态库的创建和使用
- Linux内核分析第三周
- C++第一次使用
- 顺序表的几种实现
- 粗谈《Java与模式》一书
- Unity3D 5 官方教程:标准着色器:内容和环境;金属与镜面工作流