码迷,mamicode.com
首页 > 其他好文 > 详细

用 CALayer 定制下载进度条控件

时间:2016-01-02 12:09:12      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

//
//  RPProgressView.h
//  CALayer定制下载进度条控件
//
//  Created by RinpeChen on 16/1/2.
//  Copyright © 2016年 rinpe. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RPProgressView : UIView
@property (nonatomic, assign) CGFloat progressValue;    // 进度值:0 ~ 1.0
@property (nonatomic, strong) UIColor *progressColor;   // 进度条颜色
@end

 

//
//  RPProgressView.m
//  CALayer定制下载进度条控件
//
//  Created by RinpeChen on 16/1/2.
//  Copyright © 2016年 rinpe. All rights reserved.
//

#import "RPProgressView.h"

@interface RPProgressView ()

@property (nonatomic, strong) CALayer *progressLayer;

@end

@implementation RPProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化layer
        self.progressLayer = [CALayer layer];
        self.progressLayer.backgroundColor = [UIColor redColor].CGColor;
        self.progressLayer.frame = CGRectMake(0, 0, 0, frame.size.height);
        
        // 添加子layer
        [self.layer addSublayer:self.progressLayer];
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    self.progressLayer.frame = CGRectMake(0, 0, 0, frame.size.height);
}

// 设置进度值
- (void)setProgressValue:(CGFloat)progressValue
{
    _progressValue = progressValue;
    
    if (progressValue <= 0) {
        self.progressLayer.frame = CGRectMake(0, 0, 0, self.frame.size.height);
    } else if (progressValue <= 1) {
        self.progressLayer.frame = CGRectMake(0, 0, self.frame.size.width * progressValue, self.frame.size.height);
    } else {
        self.progressLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }
}

// 设置进度条颜色
- (void)setProgressColor:(UIColor *)progressColor
{
    _progressColor = progressColor;
    
    self.progressLayer.backgroundColor = progressColor.CGColor;
}

@end

 

demo:https://github.com/RinpeChen/progressViewDemoByRinpe.git

用 CALayer 定制下载进度条控件

标签:

原文地址:http://www.cnblogs.com/Rinpe/p/5094358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!