1、绘制下载进度按钮

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

  • QProgressButton.h

    	@interface QProgressButton : UIButton
    
    	/// 进度值,范围 0 ~ 1
    @property (nonatomic, assign) CGFloat progress; /// 进度终止状态标题,一旦设置了此标题进度条就会停止
    @property (nonatomic, strong) NSString *stopTitle; /**
    * 创建带进度条的按钮
    *
    * @param frame 按钮的 frame 值
    * @param title 进按钮的标题
    * @param lineWidth 进度条的线宽,default is 2
    * @param lineColor 进度条线的颜色,default is greenColor
    * @param textColor 进度值的颜色,default is blackColor
    * @param backColor 按钮的背景颜色,default is clearColor
    * @param isRound 按钮是否显示为圆形,default is YES
    *
    * @return 带进度条的按钮
    */
    + (instancetype)q_progressButtonWithFrame:(CGRect)frame
    title:(NSString *)title
    lineWidth:(CGFloat)lineWidth
    lineColor:(nullable UIColor *)lineColor
    textColor:(nullable UIColor *)textColor
    backColor:(nullable UIColor *)backColor
    isRound:(BOOL)isRound; @end
  • QProgressButton.m

    	@interface QProgressButton ()
    
    	/// 进度条的线宽
    @property (nonatomic, assign) CGFloat lineWidth; /// 进度条线的颜色
    @property (nonatomic, strong) UIColor *lineColor; /// 按钮的背景颜色
    @property (nonatomic, strong) UIColor *backColor; /// 按钮是否显示为圆形
    @property (nonatomic, assign, getter=isRound) BOOL round; @end @implementation QProgressButton /// 创建带进度条的按钮
    + (instancetype)q_progressButtonWithFrame:(CGRect)frame
    title:(NSString *)title
    lineWidth:(CGFloat)lineWidth
    lineColor:(nullable UIColor *)lineColor
    textColor:(nullable UIColor *)textColor
    backColor:(nullable UIColor *)backColor
    isRound:(BOOL)isRound { QProgressButton *progressButton = [[self alloc] init]; progressButton.lineWidth = lineWidth ? : 2;
    progressButton.lineColor = lineColor ? : [UIColor colorWithRed:76/255.0 green:217/255.0 blue:100/255.0 alpha:1.0];
    progressButton.backColor = backColor ? : [UIColor clearColor];
    progressButton.round = isRound; // 设置按钮的实际 frame
    if (isRound) {
    CGRect tmpFrame = frame;
    tmpFrame.origin.y = frame.origin.y - (frame.size.width - frame.size.height) * 0.5;
    tmpFrame.size.height = frame.size.width;
    progressButton.frame = tmpFrame;
    } else {
    progressButton.frame = frame;
    } // 设置显示的标题和颜色
    [progressButton setTitle:title forState:UIControlStateNormal];
    [progressButton setTitleColor:(textColor ? : [UIColor blackColor]) forState:UIControlStateNormal]; return progressButton;
    } /// 绘制进度条
    - (void)drawRect:(CGRect)rect { // 设置按钮圆角
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = rect.size.height * 0.5; // 绘制按钮的背景颜色
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    [self.backColor set];
    [path fill]; // 设置进度终止时显示的内容
    if (self.stopTitle) { // 设置下载完成后的标题
    [self setTitle:self.stopTitle forState:UIControlStateNormal];
    return;
    } if (self.progress <= 0) {
    return;
    } // 清除按钮背景图片
    [self setBackgroundImage:nil forState:UIControlStateNormal]; // 设置进度值
    [self setTitle:[NSString stringWithFormat:@"%.2f%%", self.progress * 100] forState:UIControlStateNormal]; if (self.isRound) { CGPoint center = CGPointMake(rect.size.height * 0.5, rect.size.height * 0.5);
    CGFloat radius = (rect.size.height - self.lineWidth) * 0.5;
    CGFloat startA = - M_PI_2;
    CGFloat endA = startA + self.progress * 2 * M_PI; // 绘制进度条背景
    path = [UIBezierPath bezierPathWithArcCenter:center
    radius:radius
    startAngle:0
    endAngle:2 * M_PI
    clockwise:YES];
    [[[UIColor lightGrayColor] colorWithAlphaComponent:0.5] set];
    path.lineWidth = self.lineWidth;
    [path stroke]; // 绘制进度条
    path = [UIBezierPath bezierPathWithArcCenter:center
    radius:radius
    startAngle:startA
    endAngle:endA
    clockwise:YES];
    path.lineWidth = self.lineWidth;
    path.lineCapStyle = kCGLineCapRound;
    [self.lineColor set];
    [path stroke]; } else { CGFloat w = self.progress * rect.size.width;
    CGFloat h = rect.size.height; // 绘制进度条背景
    path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    [[[UIColor lightGrayColor] colorWithAlphaComponent:0.5] set];
    [path fill]; // 绘制进度条
    path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, w, h)];
    [self.lineColor set];
    [path fill];
    }
    } /// 设置进度值
    - (void)setProgress:(CGFloat)progress { _progress = progress; [self setNeedsDisplay];
    } /// 设置进度终止状态标题
    - (void)setStopTitle:(NSString *)stopTitle { _stopTitle = stopTitle; [self setNeedsDisplay];
    } @end
  • ViewController.m

    	// 创建进度按钮
    QProgressButton *progressButton = [QProgressButton q_progressButtonWithFrame:CGRectMake(100, 100, 100, 50)
    title:@"开始下载"
    lineWidth:10
    lineColor:[UIColor blueColor]
    textColor:[UIColor redColor]
    backColor:[UIColor yellowColor]
    isRound:YES]; // 设置按钮点击事件
    [progressButton addTarget:self action:@selector(progressUpdate:) forControlEvents:UIControlEventTouchUpInside]; // 将按钮添加到当前控件显示
    [self.view addSubview:progressButton]; // 设置按钮的进度值
    self.progressButton.progress = progress; // 设置按钮的进度终止标题,一旦设置了此标题进度条就会停止
    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. HDU [P1151] Air Raid

    二分图匹配求DAG图上的最小路径覆盖 应用了拆点的思想,将DAG图上的每一个点拆成二分图的x集合与y集合,对于一条有向边u->v来说,我们在ux与vy之间连一条边,然后求二分图的最大匹配 DAG ...

  2. 济南清北学堂游记 Day 1.

    快住手!这根本不是暴力! 刷了一整天的题就是了..上午三道题的画风还算挺正常,估计是第一天,给点水题做做算了.. rqy大佬AK了上午的比赛! 当时我t2暴力写挂,还以为需要用啥奇怪的算法,后来发现, ...

  3. BZOJ 3624: [Apio2008]免费道路 [生成树 并查集]

    题意: 一张图0,1两种边,构造一个恰有k条0边的生成树 优先选择1边构造生成树,看看0边是否小于k 然后保留这些0边,补齐k条,再加1边一定能构成生成树 类似kruskal的证明 #include ...

  4. bootloader总体操作设计

    bootloarder设计蓝图(不想做设计师的程序员不是好程序员):bootloarder的作用:将linux内核启动起来设计方法:模仿u-bootu-boot:支持多种嵌入式cpu的bootloar ...

  5. WPF字典集合类ObservableDictionary

    WPF最核心的技术优势之一就是数据绑定.数据绑定,可以通过对数据的操作来更新界面. 数据绑定最经常用到的是ObservableCollection<T> 和 Dictionary<T ...

  6. Hadoop2.7.3+Spark2.1.0 完全分布式环境 搭建全过程

    一.修改hosts文件 在主节点,就是第一台主机的命令行下; vim /etc/hosts 我的是三台云主机: 在原文件的基础上加上; ip1 master worker0 namenode ip2 ...

  7. python配置apache的web服务器方法(python的CGI配置)

    先大概介绍一下:Python CGI编程 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运 ...

  8. Zabbix监控之迁移zabbix server

    abbix监控中有时会根据需要对zabbix服务器进行迁移,zabbix迁移是非常简单的,因为zabbix的前端所有的操作都存在zabbix数据库里.所以zabbix迁移只需对zabbix库中相应的表 ...

  9. 9、flask之SQLAlchemy

    本篇导航: 介绍 使用 SQLAlchemy-Utils 一. 介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之 ...

  10. C/C++语言简介之优缺点

    一.优点1.简洁紧凑.灵活方便 C语言一共只有32个关键字,9种控制语句,程序书写形式自由,区分大小写.把高级语言的基本结构和语句与低级语言的实用性结合起来.C 语言可以像汇编语言一样对位.字节和地址 ...