1、绘制下载进度按钮

  • 具体实现代码见 GitHub 源码 QExtension

  • QProgressButton.h

    1. @interface QProgressButton : UIButton
    2. /// 进度值,范围 0 ~ 1
    3. @property (nonatomic, assign) CGFloat progress;
    4. /// 进度终止状态标题,一旦设置了此标题进度条就会停止
    5. @property (nonatomic, strong) NSString *stopTitle;
    6. /**
    7. * 创建带进度条的按钮
    8. *
    9. * @param frame 按钮的 frame 值
    10. * @param title 进按钮的标题
    11. * @param lineWidth 进度条的线宽,default is 2
    12. * @param lineColor 进度条线的颜色,default is greenColor
    13. * @param textColor 进度值的颜色,default is blackColor
    14. * @param backColor 按钮的背景颜色,default is clearColor
    15. * @param isRound 按钮是否显示为圆形,default is YES
    16. *
    17. * @return 带进度条的按钮
    18. */
    19. + (instancetype)q_progressButtonWithFrame:(CGRect)frame
    20. title:(NSString *)title
    21. lineWidth:(CGFloat)lineWidth
    22. lineColor:(nullable UIColor *)lineColor
    23. textColor:(nullable UIColor *)textColor
    24. backColor:(nullable UIColor *)backColor
    25. isRound:(BOOL)isRound;
    26. @end
  • QProgressButton.m

    1. @interface QProgressButton ()
    2. /// 进度条的线宽
    3. @property (nonatomic, assign) CGFloat lineWidth;
    4. /// 进度条线的颜色
    5. @property (nonatomic, strong) UIColor *lineColor;
    6. /// 按钮的背景颜色
    7. @property (nonatomic, strong) UIColor *backColor;
    8. /// 按钮是否显示为圆形
    9. @property (nonatomic, assign, getter=isRound) BOOL round;
    10. @end
    11. @implementation QProgressButton
    12. /// 创建带进度条的按钮
    13. + (instancetype)q_progressButtonWithFrame:(CGRect)frame
    14. title:(NSString *)title
    15. lineWidth:(CGFloat)lineWidth
    16. lineColor:(nullable UIColor *)lineColor
    17. textColor:(nullable UIColor *)textColor
    18. backColor:(nullable UIColor *)backColor
    19. isRound:(BOOL)isRound {
    20. QProgressButton *progressButton = [[self alloc] init];
    21. progressButton.lineWidth = lineWidth ? : 2;
    22. progressButton.lineColor = lineColor ? : [UIColor colorWithRed:76/255.0 green:217/255.0 blue:100/255.0 alpha:1.0];
    23. progressButton.backColor = backColor ? : [UIColor clearColor];
    24. progressButton.round = isRound;
    25. // 设置按钮的实际 frame
    26. if (isRound) {
    27. CGRect tmpFrame = frame;
    28. tmpFrame.origin.y = frame.origin.y - (frame.size.width - frame.size.height) * 0.5;
    29. tmpFrame.size.height = frame.size.width;
    30. progressButton.frame = tmpFrame;
    31. } else {
    32. progressButton.frame = frame;
    33. }
    34. // 设置显示的标题和颜色
    35. [progressButton setTitle:title forState:UIControlStateNormal];
    36. [progressButton setTitleColor:(textColor ? : [UIColor blackColor]) forState:UIControlStateNormal];
    37. return progressButton;
    38. }
    39. /// 绘制进度条
    40. - (void)drawRect:(CGRect)rect {
    41. // 设置按钮圆角
    42. self.layer.masksToBounds = YES;
    43. self.layer.cornerRadius = rect.size.height * 0.5;
    44. // 绘制按钮的背景颜色
    45. UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    46. [self.backColor set];
    47. [path fill];
    48. // 设置进度终止时显示的内容
    49. if (self.stopTitle) {
    50. // 设置下载完成后的标题
    51. [self setTitle:self.stopTitle forState:UIControlStateNormal];
    52. return;
    53. }
    54. if (self.progress <= 0) {
    55. return;
    56. }
    57. // 清除按钮背景图片
    58. [self setBackgroundImage:nil forState:UIControlStateNormal];
    59. // 设置进度值
    60. [self setTitle:[NSString stringWithFormat:@"%.2f%%", self.progress * 100] forState:UIControlStateNormal];
    61. if (self.isRound) {
    62. CGPoint center = CGPointMake(rect.size.height * 0.5, rect.size.height * 0.5);
    63. CGFloat radius = (rect.size.height - self.lineWidth) * 0.5;
    64. CGFloat startA = - M_PI_2;
    65. CGFloat endA = startA + self.progress * 2 * M_PI;
    66. // 绘制进度条背景
    67. path = [UIBezierPath bezierPathWithArcCenter:center
    68. radius:radius
    69. startAngle:0
    70. endAngle:2 * M_PI
    71. clockwise:YES];
    72. [[[UIColor lightGrayColor] colorWithAlphaComponent:0.5] set];
    73. path.lineWidth = self.lineWidth;
    74. [path stroke];
    75. // 绘制进度条
    76. path = [UIBezierPath bezierPathWithArcCenter:center
    77. radius:radius
    78. startAngle:startA
    79. endAngle:endA
    80. clockwise:YES];
    81. path.lineWidth = self.lineWidth;
    82. path.lineCapStyle = kCGLineCapRound;
    83. [self.lineColor set];
    84. [path stroke];
    85. } else {
    86. CGFloat w = self.progress * rect.size.width;
    87. CGFloat h = rect.size.height;
    88. // 绘制进度条背景
    89. path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    90. [[[UIColor lightGrayColor] colorWithAlphaComponent:0.5] set];
    91. [path fill];
    92. // 绘制进度条
    93. path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, w, h)];
    94. [self.lineColor set];
    95. [path fill];
    96. }
    97. }
    98. /// 设置进度值
    99. - (void)setProgress:(CGFloat)progress {
    100. _progress = progress;
    101. [self setNeedsDisplay];
    102. }
    103. /// 设置进度终止状态标题
    104. - (void)setStopTitle:(NSString *)stopTitle {
    105. _stopTitle = stopTitle;
    106. [self setNeedsDisplay];
    107. }
    108. @end
  • ViewController.m

    1. // 创建进度按钮
    2. QProgressButton *progressButton = [QProgressButton q_progressButtonWithFrame:CGRectMake(100, 100, 100, 50)
    3. title:@"开始下载"
    4. lineWidth:10
    5. lineColor:[UIColor blueColor]
    6. textColor:[UIColor redColor]
    7. backColor:[UIColor yellowColor]
    8. isRound:YES];
    9. // 设置按钮点击事件
    10. [progressButton addTarget:self action:@selector(progressUpdate:) forControlEvents:UIControlEventTouchUpInside];
    11. // 将按钮添加到当前控件显示
    12. [self.view addSubview:progressButton];
    13. // 设置按钮的进度值
    14. self.progressButton.progress = progress;
    15. // 设置按钮的进度终止标题,一旦设置了此标题进度条就会停止
    16. self.progressButton.stopTitle = @"下载完成";
  • 效果

iOS - Quartz 2D 下载进度按钮绘制的更多相关文章

  1. iOS - Quartz 2D 第三方框架 Charts 绘制图表

    1.Charts 简介 使用第三方框架 Charts 绘制 iOS 图表.GitHub 源码 Charts Charts 是一款用于绘制图表的框架,可以绘制柱状图.折线图.K线图.饼状图等.Chart ...

  2. iOS - Quartz 2D 手势截屏绘制

    1.绘制手势截屏 具体实现代码见 GitHub 源码 QExtension QTouchClipView.h @interface QTouchClipView : UIView /** * 创建手势 ...

  3. iOS - Quartz 2D 二维绘图

    1.Quartz 2D 简介 Quartz 2D 属于 Core Graphics(所以大多数相关方法的都是以 CG 开头),是 iOS/Mac OSX 提供的在内核之上的强大的 2D 绘图引擎,并且 ...

  4. iOS - Quartz 2D 贝塞尔曲线

    1.贝塞尔曲线 贝塞尔曲线(Bézier curve),又称贝兹曲线或贝济埃曲线,是应用于二维图形应用程序的数学曲线.一般的矢量图形软件通过它来精确画出曲线,贝兹曲线由线段与节点组成,节点是可拖动的支 ...

  5. iOS空心圆下载进度指示器控件

    self.layer = [CAShapeLayer layer]; self.layer.frame = CGRectMake(, , , ); self.layer.position = self ...

  6. iOS - Quartz 2D 画板绘制

    1.绘制画板 1.1 绘制简单画板 PaintBoardView.h @interface PaintBoardView : UIView @end PaintBoardView.m @interfa ...

  7. IOS Quartz 2D 学习(1)

    IOS提供两种创建图形的途径: 1.OpenGL. 2.Quartz.Core Animation.UIKit图形支持. UIKit的图形系统 1.视图绘画周期: DrawRect方法,在任何时候,当 ...

  8. iOS开发——图层OC篇&Quartz 2D各种绘制实例

    Quartz 2D各种绘制实例 首先说一下,本篇文章只是介绍怎么使用Quartz 2D绘制一些常用的图像效果,关于Quartz和其他相关技术请查看笔者之前写的完整版(Quartz 2D详解) 一:画线 ...

  9. iOS 2D绘图 (Quartz 2D) 概述

    本篇博客原文地址:http://blog.csdn.net/hello_hwc?viewmode=list 由于自己的项目需要,从网络上下载了许多关于绘制图形的demo,只是用在自己的项目中,很多地方 ...

随机推荐

  1. [快速傅立叶变换&快速傅里叶变换]【旧 手写笔记】

    $FFT$好美啊 参考资料: 1.算法导论 2.Miskcoo 3.Menci 4.虚数的意义-阮一峰 简单说一下,具体在下面的图片 实现: 可以用$complex$也可以手写 和计算几何差不多 注意 ...

  2. BZOJ 1076: [SCOI2008]奖励关 [DP 期望 状压]

    传送门 题意:$n$种宝物,出现$k$次每次一种,每种宝物有价值和吃掉它之前必须要吃掉的宝物的集合,求采取最优策略的期望最大价值 1<=k<=100,1<=n<=15,分值为[ ...

  3. 孤立的SQL用户

    问题 最近公司很多数据库在上云,也有一部分在下云.这期间出现了很多问题,其中一个比较恶心的问题就是"孤立用户".当数据库备份还原以后用以前的用户发现不能登录.一开始以为是登录账号没 ...

  4. install atom markdown preview plus error

    Installing "markdown-preview-enhanced@0.15.2" failed.Hide output- npm ERR! Darwin 17.2.0 n ...

  5. 关于Git的版本问题

    问题的起源 我在IDEA上不小心修改了文件(加了一行空行)并且被保存了,在GitHub Desktop桌面工具上可以看到changes中有修改记录,并且使用命令行git status也可以看到文件的修 ...

  6. CENTOS/RHEL 7 系统中设置SYSTEMD SERVICE的ULIMIT资源限制

    遇到的问题: golang程序一直出现 too many open files的报错, 尽管对 /etc/security/limits.conf 做了设置, 对最大文件打开数,最大进程数做了调优. ...

  7. nginx/php-fpm 访问php文件直接下载而不运行

    遇到这种问题,首先确认你web服务器配置中的.PHP是不是被指定给FastCGI server处理: location ~ .php$ { fastcgi_pass ; } 如已配置,那么可能是由于f ...

  8. Git 如何 clone 非 master 分支的代码

    问题描述 我们每次使用命令 git clone git@gitlab.xxx.com:xxxxx.git 默认 clone 的是这个仓库的 master 分支.如果最新的代码不在 master 分支上 ...

  9. MTU介绍以及在windows和linux下怎么设置MTU值

    最大传输单元MTU(Maximum Transmission Unit,MTU)是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位).最大传输单元这个参数通常与通信接口有关(网络接口卡 ...

  10. 你所有不知的margin属性

    前言 致谢 本文总结于 张鑫旭老师的 CSS深入理解之margin课程,感谢张老师的辛苦付出! 难学的 CSS 作为前端狗的我们,每天都要和网页打交道.当 UI 将设计稿发给你时,CSS 的知识便显得 ...