用CALayer实现下载进度条

效果:

源码:

  1. //
  2. // ViewController.m
  3. // ProgressView
  4. //
  5. // Created by YouXianMing on 14/11/18.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12.  
  13. @property (nonatomic, strong) NSTimer *timer;
  14. @property (nonatomic, strong) CALayer *layer;
  15.  
  16. @end
  17.  
  18. @implementation ViewController
  19.  
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22.  
  23. self.view.backgroundColor = [UIColor blackColor];
  24.  
  25. // CALayer
  26. _layer = [CALayer layer];
  27. _layer.frame = CGRectMake(, , , );
  28. _layer.backgroundColor = [UIColor redColor].CGColor;
  29. [self.view.layer addSublayer:_layer];
  30.  
  31. // 定时器
  32. _timer = [NSTimer scheduledTimerWithTimeInterval:.f
  33. target:self
  34. selector:@selector(timerEvent)
  35. userInfo:nil
  36. repeats:YES];
  37. }
  38.  
  39. /**
  40. * 定时器
  41. */
  42. - (void)timerEvent {
  43. // 显式动画
  44. _layer.frame = CGRectMake(, , arc4random()%, );
  45. }
  46.  
  47. @end

封装后的源码:

  1. //
  2. // ProgressView.h
  3. // ProgressView
  4. //
  5. // Created by YouXianMing on 14/11/18.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface ProgressView : UIView
  12.  
  13. @property (nonatomic, strong) UIColor *progressColor;
  14. @property (nonatomic, assign) CGFloat progress;
  15.  
  16. @end
  1. //
  2. // ProgressView.m
  3. // ProgressView
  4. //
  5. // Created by YouXianMing on 14/11/18.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "ProgressView.h"
  10.  
  11. @interface ProgressView ()
  12.  
  13. {
  14. CALayer *_backLayer;
  15. CGFloat _frameWidth;
  16. CGFloat _layerWidth;
  17. }
  18.  
  19. @end
  20.  
  21. @implementation ProgressView
  22.  
  23. - (instancetype)initWithFrame:(CGRect)frame
  24. {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. _backLayer = [CALayer layer];
  28.  
  29. _frameWidth = self.bounds.size.width;
  30. _layerWidth = ;
  31.  
  32. _backLayer.frame = CGRectMake(, , _layerWidth, frame.size.height);
  33. [self.layer addSublayer:_backLayer];
  34. }
  35. return self;
  36. }
  37.  
  38. @synthesize progressColor = _progressColor;
  39. - (void)setProgressColor:(UIColor *)progressColor {
  40. _progressColor = progressColor;
  41. _backLayer.backgroundColor = _progressColor.CGColor;
  42. }
  43. - (UIColor *)progressColor {
  44. return _progressColor;
  45. }
  46.  
  47. @synthesize progress = _progress;
  48. - (void)setProgress:(CGFloat)progress {
  49. _progress = progress;
  50.  
  51. CGRect rect = _backLayer.frame;
  52. if (_progress <= ) {
  53. rect.size.width = ;
  54. } else if (_progress >= ) {
  55. rect.size.width = _frameWidth;
  56. } else {
  57. rect.size.width = _progress * _frameWidth;
  58. }
  59.  
  60. _backLayer.frame = rect;
  61. }
  62. - (CGFloat)progress {
  63. return _progress;
  64. }
  65.  
  66. @end

用CALayer实现下载进度条控件的更多相关文章

  1. 用 CALayer 定制下载进度条控件

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

  2. Photoshop和WPF双剑配合,打造炫酷个性的进度条控件

    现在如果想打造一款专业的App,UI的设计和操作的简便性相当重要.UI设计可以借助Photoshop或者AI等设计工具,之前了解到WPF设计工具Expression Blend可以直接导入PSD文件或 ...

  3. HslControls组件库 工业控件库 曲线控件 时间控件 管道控件 温度计控件 阀门控件 传送带控件 进度条控件 电池控件 数码管控件等等

    本篇博客主要对 HslControls 组件做一个大概的总览介绍,更详细的内容可以参照页面里的子链接,还有github上的源代码,然后进行相关的学习,和使用. Prepare 先从nuget下载到组件 ...

  4. iOS:进度条控件的详细使用

    进度条控件:UIProcessView:UIView   功能:顾名思义,用来显示下载进度或者传输数据进度.   属性: @property(nonatomic) UIProgressViewStyl ...

  5. DevExpress的进度条控件ProgressBarControl的使用-以ZedGraph添加曲线进度为例

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  6. 示例:WPF中Slider控件封装的缓冲播放进度条控件

    原文:示例:WPF中Slider控件封装的缓冲播放进度条控件 一.目的:模仿播放器播放进度条,支持缓冲任务功能 二.进度: 实现类似播放器中带缓存的播放样式(播放区域.缓冲区域.全部区域等样式) 实现 ...

  7. Qt编写自定义控件32-等待进度条控件

    一.前言 在各种各样的执行任务界面,有时候需要比较多的时间,需要给出一个直观的等待进度条表示当前正在执行的进度,而不至于懵逼在那里,用户不会觉得程序死了还是干嘛了. 等待进度条有好几种办法,比如直接叫 ...

  8. [转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  9. ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

    本篇要登场的有三个控件,分别是滚轴控件.进度条控件和编辑控件. 一.滚轴控件 Ext.slider 1.滚轴控件的定义 下面我们定义三个具有代表意义滚轴控件,分别展示滚轴横向.纵向,以及单值.多值选择 ...

随机推荐

  1. dip,px,sp区别及使用场景

    1.区别 dip(device independent pixels)——设备独立像素:这个和设备硬件有关,一般哦我们为了支持WCGA.HVGA和QVGA推荐使用这个,不依赖于像素.等同于dp. px ...

  2. Django的国际化

    Django的国际化 Django基于python的gettext来实现国际化.其过程为安装gettext工具,然后再代码中调用指定方法,然后用gettext提供的程序来把代码中需要翻译的部分找出来写 ...

  3. Wireshark命令行工具tshark详解(含例子)-01

    Wireshark命令行工具tshark使用小记 1.目的 写这篇博客的目的主要是为了方便查阅,使用wireshark可以分析数据包,可以通过编辑过滤表达式来达到对数据的分析:但我的需求是,怎么样把D ...

  4. Windows7 x64 了解堆

    一.前言 堆对于开发者一般来说是熟悉又陌生的,熟悉是因为我们常常使用new/delete或者malloc/free使用堆,陌生是因为我们基本没有去了解堆的结构.堆在什么地方?怎么申请?怎么释放?系统又 ...

  5. 利用jquery的ajax实现跨域,内部其实是jsonp协议了,不是XHRhttp协议

    一.同源策略 要理解跨域,先要了解一下“同源策略”.所谓同源是指,域名,协议,端口相同.所谓“同源策略“,简单的说就是基于安全考虑,当前域不能访问其他域的东西. 一些常见的是否同源示例可参照下表: 在 ...

  6. 【AngularJs】---JSONP跨域访问数据传输(JSON_CALLBACK)

    大家会自然想到只有一个字母之差的JSON吧~ JSON(JavaScript Object Notation)和JSONP(JSON with Padding)虽然只有一个字母的差别,但其实他们根本不 ...

  7. 撩课-Web大前端每天5道面试题-Day13

    1.前端需要注意哪些SEO? 合理的title.description.keywords:搜索对着三项的权重逐个减小,title值强调重点即可,重要关键词出现不要超过2次,而且要靠前,不同页面titl ...

  8. 项目中遇到的问题——jsp:include

    昨晚记错了,项目中用的是这个<jsp:attribute>,不过没关系,都差不多!原理是传参数 具体用法: 假设有两个tag文件  aaa 和 bbb aaa有两个属性:name  age ...

  9. 转:zip 和 unzip 的参数说明

    收集的资料: 1. 我想把一个文件abc.txt和一个目录dir1压缩成为yasuo.zip: # zip -r yasuo.zip abc.txt dir1 2.我下载了一个yasuo.zip文件, ...

  10. hdu 1159(DP+字符串最长公共序列)

    http://blog.csdn.net/a_eagle/article/details/7213236 公共序列可以用一个二维数组dp[i][j]保存每个点时的最大数字,本质就是一个双向比较. dp ...