博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用CALayer实现下载进度条控件
阅读量:5248 次
发布时间:2019-06-14

本文共 2773 字,大约阅读时间需要 9 分钟。

用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 
@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

你可能感兴趣的文章
emlog在nginx中添加rewrite规则
查看>>
初识Iaas,paas
查看>>
查看oracle 启动了多久
查看>>
Mina自定义编解码
查看>>
.NET 开源了,Visual Studio 开始支持 Android 和 iOS 程序编写并自带 Android 模拟器【转载】...
查看>>
Linux下chkconfig命令详解
查看>>
第一天开通博客纪念一下
查看>>
Android通过IO流获取服务器的图片数据
查看>>
取模运算的两种模式
查看>>
第五天(学习笔记)
查看>>
Swift - 委托(delegate)5秒看懂
查看>>
RSA
查看>>
celery
查看>>
yii2 linux安装教程
查看>>
android--------自定义视频控件(视频全屏竖屏自动切换)
查看>>
使用Fiddler工具在夜神模拟器或手机上抓包
查看>>
C#变量初始化问题:字段初始值无法引用非静态字段、方法或属性
查看>>
一次性搞定Session
查看>>
矩阵与列表取出行(左开右闭)
查看>>
JDBC实现Oracle数据库的增删改查
查看>>