• 用 CALayer 定制下载进度条控件


    //
    //  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

  • 相关阅读:
    关于JSONP
    使用stylelint对CSS/Sass做代码审查
    关于input的file框onchange事件触发一次失效的新的解决方法
    HTML5 之 FileReader(图片上传)
    document.domain
    window.hostory(浏览器的历史记录)
    事件DOMContentLoaded和load的区别
    JavaScript中---作用域
    关于repaint(重绘)和reflow( 回流)
    bootstrap兼容性问题
  • 原文地址:https://www.cnblogs.com/Rinpe/p/5094358.html
Copyright © 2020-2023  润新知