简书也有发布:http://www.jianshu.com/p/20d7...

《iOS球形波浪加载进度控件-HcdProcessView》这篇文章已经展示了我在项目中编写的一个球形进度加载控件HcdProcessView,这篇文章我要简单介绍一下我的制作过程。

思路

首先我放弃了使用通过改变图片的位置来实现上面的动画效果,虽然这样也可以实现如上的效果,但是从性能和资源消耗上来说都不是最好的选择。这里我采用了通过上下文(也就是CGContextRef)来绘制这样的效果,大家对它应该并不陌生,它既可以绘制直线、曲线、多边形圆形以及各种各样的几何图形。

具体步骤

我们可以将上面的复杂图形拆分成如下几步:

  1. 绘制最外面的一圈刻度尺

  2. 绘制表示进度的刻度尺

  3. 绘制中间的球形加载界面

绘制刻度尺

如果你先要在控件中绘制自己想要的图形,你需要重写UIView的drawRect方法:

- (void)drawRect:(CGRect)rect
{CGContextRef context = UIGraphicsGetCurrentContext();[self drawScale:context];
}

drawRect方法中,我们先画出了刻度尺的图形,刻度尺是由一圈短线在一个圆内围成的一个圆。

/***  画比例尺**  @param context 全局context*/
- (void)drawScale:(CGContextRef)context {CGContextSetLineWidth(context, _scaleDivisionsWidth);//线的宽度//先将参照点移到控件中心CGContextTranslateCTM(context, fullRect.size.width / 2, fullRect.size.width / 2);//设置线的颜色CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.655 green:0.710 blue:0.859 alpha:1.00].CGColor);//线框颜色//绘制一些图形for (int i = 0; i < _scaleCount; i++) {CGContextMoveToPoint(context, scaleRect.size.width/2 - _scaleDivisionsLength, 0);CGContextAddLineToPoint(context, scaleRect.size.width/2, 0);//    CGContextScaleCTM(ctx, 0.5, 0.5);//渲染CGContextStrokePath(context);CGContextRotateCTM(context, 2 * M_PI / _scaleCount);}//绘制刻度尺外的一个圈CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.694 green:0.745 blue:0.867 alpha:1.00].CGColor);//线框颜色CGContextSetLineWidth(context, 0.5);CGContextAddArc (context, 0, 0, scaleRect.size.width/2 - _scaleDivisionsLength - 3, 0, M_PI* 2 , 0);CGContextStrokePath(context);//复原参照点CGContextTranslateCTM(context, -fullRect.size.width / 2, -fullRect.size.width / 2);
}

这里需要用到两个东西一个是CGContextAddArc,一个是CGContextAddLineToPoint。创建圆弧的方法有两种一种是CGContextAddArc,一种是CGContextAddArcToPoint,这里画的圆比较简单所以用的是CGContextAddArc,CGContextAddArcToPoint在后面也会用到(我会在用到的地方详解)。

CGContextAddArc

 void CGContextAddArc (CGContextRef c,    CGFloat x,             //圆心的x坐标CGFloat y,   //圆心的x坐标CGFloat radius,   //圆的半径 CGFloat startAngle,    //开始弧度CGFloat endAngle,   //结束弧度int clockwise          //0表示顺时针,1表示逆时针);

这里需要创建一个完整的圆,那么 开始弧度就是0 结束弧度是 2PI, 因为圆周长是 2PIradius。函数执行完后,current point就被重置为(x,y)。CGContextTranslateCTM(context, fullRect.size.width / 2, fullRect.size.width / 2);已经将current point移动到了(fullRect.size.width / 2, fullRect.size.width / 2)

CGContextAddLineToPoint

 void CGContextAddLineToPoint (CGContextRef c,CGFloat x,CGFloat y);

创建一条直线,从current point到 (x,y)
然后current point会变成(x,y)。
由于短线不连续,所以通过for循环来不断画短线,_scaleCount代表的是刻度尺的个数,每次循环先将current point移动到(scaleRect.size.width/2 - _scaleDivisionsLength, 0)点,_scaleDivisionsLength代表短线的长度。绘制完短线后将前面绘制完成的图形旋转一个刻度尺的角度CGContextRotateCTM(context, 2 * M_PI / _scaleCount);,将最终的绘制渲染后就得到了如下的刻度尺:

刻度尺上的进度绘制

首先在drawRect中添加drawProcessScale方法。

- (void)drawRect:(CGRect)rect
{CGContextRef context = UIGraphicsGetCurrentContext();[self drawScale:context];[self drawProcessScale:context];
}

然后在drawProcessScale方法中实现左右两部分的刻度尺进度绘制。

/***  比例尺进度**  @param context 全局context*/
- (void)drawProcessScale:(CGContextRef)context {CGContextSetLineWidth(context, _scaleDivisionsWidth);//线的宽度CGContextTranslateCTM(context, fullRect.size.width / 2, fullRect.size.width / 2);CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.969 green:0.937 blue:0.227 alpha:1.00].CGColor);//线框颜色int count = (_scaleCount / 2 + 1) * currentPercent;CGFloat scaleAngle = 2 * M_PI / _scaleCount;//绘制左边刻度进度for (int i = 0; i < count; i++) {CGContextMoveToPoint(context, 0, scaleRect.size.width/2 - _scaleDivisionsLength);CGContextAddLineToPoint(context, 0, scaleRect.size.width/2);//    CGContextScaleCTM(ctx, 0.5, 0.5);// 渲染CGContextStrokePath(context);CGContextRotateCTM(context, scaleAngle);}//绘制右边刻度进度CGContextRotateCTM(context, -count * scaleAngle);for (int i = 0; i < count; i++) {CGContextMoveToPoint(context, 0, scaleRect.size.width/2 - _scaleDivisionsLength);CGContextAddLineToPoint(context, 0, scaleRect.size.width/2);//    CGContextScaleCTM(ctx, 0.5, 0.5);// 渲染CGContextStrokePath(context);CGContextRotateCTM(context, -scaleAngle);}CGContextTranslateCTM(context, -fullRect.size.width / 2, -fullRect.size.width / 2);
}

绘制完后效果如下:

水的波浪效果绘制

终于到了最主要也是最难的效果绘制了,对于带有波浪不断滚动的效果是采用NSTimer来不断绘制每一帧图形实现的,现在简单介绍下每一帧的绘制方法。
首先在drawRect中添加drawWave方法,

- (void)drawRect:(CGRect)rect
{CGContextRef context = UIGraphicsGetCurrentContext();[self drawScale:context];[self drawProcessScale:context];[self drawWave:context];
}

drawWave中实现如下方法:

/***  画波浪**  @param context 全局context*/
- (void)drawWave:(CGContextRef)context {CGMutablePathRef frontPath = CGPathCreateMutable();CGMutablePathRef backPath = CGPathCreateMutable();//画水CGContextSetLineWidth(context, 1);CGContextSetFillColorWithColor(context, [_frontWaterColor CGColor]);CGFloat offset = _scaleMargin + _waveMargin + _scaleDivisionsWidth;float frontY = currentLinePointY;float backY = currentLinePointY;CGFloat radius = waveRect.size.width / 2;CGPoint frontStartPoint = CGPointMake(offset, currentLinePointY + offset);CGPoint frontEndPoint = CGPointMake(offset, currentLinePointY + offset);CGPoint backStartPoint = CGPointMake(offset, currentLinePointY + offset);CGPoint backEndPoint = CGPointMake(offset, currentLinePointY + offset);for(float x = 0; x <= waveRect.size.width; x++){//前浪绘制frontY = a * sin( x / 180 * M_PI + 4 * b / M_PI ) * amplitude + currentLinePointY;CGFloat frontCircleY = frontY;if (currentLinePointY < radius) {frontCircleY = radius - sqrt(pow(radius, 2) - pow((radius - x), 2));if (frontY < frontCircleY) {frontY = frontCircleY;}} else if (currentLinePointY > radius) {frontCircleY = radius + sqrt(pow(radius, 2) - pow((radius - x), 2));if (frontY > frontCircleY) {frontY = frontCircleY;}}if (fabs(0 - x) < 0.001) {frontStartPoint = CGPointMake(x + offset, frontY + offset);CGPathMoveToPoint(frontPath, NULL, frontStartPoint.x, frontStartPoint.y);}frontEndPoint = CGPointMake(x + offset, frontY + offset);CGPathAddLineToPoint(frontPath, nil, frontEndPoint.x, frontEndPoint.y);//后波浪绘制backY = a * cos( x / 180 * M_PI + 3 * b / M_PI ) * amplitude + currentLinePointY;CGFloat backCircleY = backY;if (currentLinePointY < radius) {backCircleY = radius - sqrt(pow(radius, 2) - pow((radius - x), 2));if (backY < backCircleY) {backY = backCircleY;}} else if (currentLinePointY > radius) {backCircleY = radius + sqrt(pow(radius, 2) - pow((radius - x), 2));if (backY > backCircleY) {backY = backCircleY;}}if (fabs(0 - x) < 0.001) {backStartPoint = CGPointMake(x + offset, backY + offset);CGPathMoveToPoint(backPath, NULL, backStartPoint.x, backStartPoint.y);}backEndPoint = CGPointMake(x + offset, backY + offset);CGPathAddLineToPoint(backPath, nil, backEndPoint.x, backEndPoint.y);}CGPoint centerPoint = CGPointMake(fullRect.size.width / 2, fullRect.size.height / 2);//绘制前浪圆弧CGFloat frontStart = [self calculateRotateDegree:centerPoint point:frontStartPoint];CGFloat frontEnd = [self calculateRotateDegree:centerPoint point:frontEndPoint];CGPathAddArc(frontPath, nil, centerPoint.x, centerPoint.y, waveRect.size.width / 2, frontEnd, frontStart, 0);CGContextAddPath(context, frontPath);CGContextFillPath(context);//推入CGContextSaveGState(context);CGContextDrawPath(context, kCGPathStroke);CGPathRelease(frontPath);//绘制后浪圆弧CGFloat backStart = [self calculateRotateDegree:centerPoint point:backStartPoint];CGFloat backEnd = [self calculateRotateDegree:centerPoint point:backEndPoint];CGPathAddArc(backPath, nil, centerPoint.x, centerPoint.y, waveRect.size.width / 2, backEnd, backStart, 0);CGContextSetFillColorWithColor(context, [_backWaterColor CGColor]);CGContextAddPath(context, backPath);CGContextFillPath(context);//推入CGContextSaveGState(context);CGContextDrawPath(context, kCGPathStroke);CGPathRelease(backPath);}

上面的代码较长,可能也比较难以理解。下面我将会对上述代码简单解读一下,已前浪为例(前浪和后浪的实现方式基本一样,只是两个浪正余弦函数不一样而已)。两个浪都是由一条曲线和和一个圆弧构成的封闭区间,曲线的x区间为[0, waveRect.size.width],y值坐标为frontY = a * sin( x / 180 * M_PI + 4 * b / M_PI ) * amplitude + currentLinePointY;(currentLinePointY为偏移量),通过for循环自增x,计算出y的位置来不断CGPathAddLineToPoint绘制出一条曲线,这就构成了波浪的曲线。然后我们需要根据波浪曲线的起始点和结束点以及圆心点(fullRect.size.width / 2, fullRect.size.height / 2),来绘制一段封闭的圆弧。
这里就需要用到CGPathAddArc方法;CGPathAddArc方法和CGContextAddArc类似。需要先计算出点波浪的起始点和结束点分别与圆心之间的夹角。知道两点计算夹角的方式如下:

/***  根据圆心点和圆上一个点计算角度**  @param centerPoint 圆心点*  @param point       圆上的一个点**  @return 角度*/
- (CGFloat)calculateRotateDegree:(CGPoint)centerPoint point:(CGPoint)point {CGFloat rotateDegree = asin(fabs(point.y - centerPoint.y) / (sqrt(pow(point.x - centerPoint.x, 2) + pow(point.y - centerPoint.y, 2))));//如果point纵坐标大于原点centerPoint纵坐标(在第一和第二象限)if (point.y > centerPoint.y) {//第一象限if (point.x >= centerPoint.x) {rotateDegree = rotateDegree;}//第二象限else {rotateDegree = M_PI - rotateDegree;}} else //第三和第四象限{if (point.x <= centerPoint.x) //第三象限,不做任何处理{rotateDegree = M_PI + rotateDegree;}else //第四象限{rotateDegree = 2 * M_PI - rotateDegree;}}return rotateDegree;
}

波浪绘制的相关判断

由于曲线x区间是[0, waveRect.size.width],y值是根据公式frontY = a * sin( x / 180 * M_PI + 4 * b / M_PI ) * amplitude + currentLinePointY;计算出来的,但是最终构成的波浪是一个球形的,所以对于计算出来的y值坐标,我们需要判断它是否在圆上,如果不在圆上,我们应该将它移到圆上。

判断分为两种情况:

currentLinePointY<fullRect.size.height / 2

当currentLinePointY<fullRect.size.height / 2时,已知点的坐标x,根据公式y1 = a * sin( x / 180 * M_PI + 4 * b / M_PI ) * amplitude + currentLinePointY;算出来的点位置为(x, y1),而在圆上点坐标为x的点的位置在(x,y2),如果y1<y2 则最终应该放到波浪上的点为 (x,y2)

currentLinePointY>fullRect.size.height / 2

同理当currentLinePointY>fullRect.size.height / 2时,已知点的坐标x,根据公式y1 = a * sin( x / 180 * M_PI + 4 * b / M_PI ) * amplitude + currentLinePointY;算出来的点位置为(x, y1),而在圆上点坐标为x的点的位置在(x,y2),如果y1>y2 则最终应该放到波浪上的点为 (x,y2)

其中判断的代码如下:

frontY = a * sin( x / 180 * M_PI + 4 * b / M_PI ) * amplitude + currentLinePointY;CGFloat frontCircleY = frontY;
if (currentLinePointY < radius) {frontCircleY = radius - sqrt(pow(radius, 2) - pow((radius - x), 2));if (frontY < frontCircleY) {frontY = frontCircleY;}
} else if (currentLinePointY > radius) {frontCircleY = radius + sqrt(pow(radius, 2) - pow((radius - x), 2));if (frontY > frontCircleY) {frontY = frontCircleY;}
}

其中当currentLinePointY < radius 时,y2=radius - sqrt(pow(radius, 2) - pow((radius - x), 2));
currentLinePointY > radius时,y2=radius + sqrt(pow(radius, 2) - pow((radius - x), 2))

这样就构成了一个如下的效果:

然后通过Timer不断的改变ab的值就得到了我想要的动画效果。

Github地址:https://github.com/Jvaeyhcd/H...

CGContextRef绘图-iOS球形波浪加载进度控件-HcdProcessView详解相关推荐

  1. 动态加载用户控件的怪问题

    动态加载用户控件的时候,会因为调用一些控件的一些属性和方法而造成控件命名混乱. 因为add 一个用户控件或者 loadcontrol 的时候 如果没有指定控件的id,clientid,那么它会初始id ...

  2. python从date目录导入数据集_PyTorch加载自己的数据集实例详解

    数据预处理在解决深度学习问题的过程中,往往需要花费大量的时间和精力. 数据处理的质量对训练神经网络来说十分重要,良好的数据处理不仅会加速模型训练, 更会提高模型性能.为解决这一问题,PyTorch提供 ...

  3. asp.net读取用户控件,自定义加载用户控件

    1.自定义加载用户控件 ceshi.aspx页面 <html><body> <div id="divControls" runat="ser ...

  4. openlayers添加按钮_OpenLayers3加载常用控件使用方法详解

    本文实例为大家分享了OpenLayers3加载常用控件使用的具体代码,供大家参考,具体内容如下 1. 前言 地图控件就是对地图的缩放.全屏.坐标显示控件等,方便我们对地图进行操作.OpenLayers ...

  5. EasyUI加载树控件自动展开所有目录

    在这里如何加载树控件就不在熬述,在加载树控件后,树的节点全部展开,要在OnLoadSuccess事件中写代码: 转载于:https://www.cnblogs.com/luyuwei/p/528003 ...

  6. VB6.0动态加载ActiveX控件漫谈[转]

    [转帖]VB6.0动态加载ActiveX控件漫谈http://www.7880.com/Info/Article-4b559560.html 熟悉VB的朋友对使用ActiveX控件一定不会陌生,众多控 ...

  7. php 无法加载activex,IE怎么无法加载 Activex 控件?

    IE怎么无法加载 Activex 控件?很多小伙伴知道ActiveX 控件是一种可重用的软件组件,通过使用 ActiveX控件,可以很快地在网址.台式应用程序.以及开发工具中加入特殊的功能.下面,小编 ...

  8. php 无法加载activex,IE无法加载 Activex 控件的解决办法

    ActiveX 控件是一种可重用的软件组件,通过使用 ActiveX控件,可以很快地在网址.台式应用程序.以及开发工具中加入特殊的功能.如,StockTicker控件可以用来在网页上即时地加入活动信息 ...

  9. linux如何确定共享库路径,摘录Linux下动态共享库加载时的搜索路径详解

    对动态库的实际应用还不太熟悉的读者可能曾经遇到过类似"error while loading shared libraries"这样的错误,这是典型的因为需要的动态库不在动态链接器 ...

最新文章

  1. 为什么阿里巴巴禁止使用Apache Beanutils进行属性的copy?
  2. [声明]关于春节回家期间不能更新Blog的说明
  3. uboot 命令分析(一) — bootm
  4. Linux 下打包报错:enospc (no space left on device)
  5. 如何:优化Hive ZooKeeper Lock Manager实施
  6. mysql 视图 动态sql_sql-server – 使用动态Sql创建视图
  7. 《APUE》第6章笔记
  8. 一文讲清数据治理、数据管理、数据资产管理区别,数据专家必看
  9. 你看,公司状告员工不加班,居然还告赢了
  10. 围绕sqlite构建一个简单的Typescript ORM
  11. 深入了解OWIN Katana
  12. 【PTA L2-012】关于堆的判断(堆的建立和特殊字符串的读入)
  13. 2020年4月,全国程序员平均工资14249元
  14. Oracle使用函数达到drop table if exists
  15. 25匹马选出最快的3匹马
  16. 数据安全--3--数据安全5A之授权
  17. 当页面放大后, 背景会消失
  18. C# socket通信 接收缓冲区大小设置,以及粘包问题的解决
  19. 华南理工大学“三七互娱杯”程序设计竞赛 A (dp)
  20. 电商网站产品数据库设计

热门文章

  1. php如何让字母加1,如何使用PHP以任何顺序(从12个字母组成6个单词组成一个字母)进行字符搜索?...
  2. python题目及解析_python知识点总结以及15道题的解析
  3. python重复元素判定_20段极简Python代码:这些小技巧你都Get了么
  4. 时间序列交叉验证TimeSeriesSplit
  5. java点到原点距离_java-从经纬度坐标到欧氏距离(EuclideanDistance)完整代码
  6. 但凭鸿蒙是什么意思,你好,鸿蒙!正式版,6月见
  7. php lararel,laravel整合tinymce和ckeditor编辑器,并用elfinder上传图片
  8. 2019春季季节跳动招聘笔试(回忆版)第二题
  9. 软件项目管理0819:一页项目管理——风险,定性问题和其他评价指标
  10. python 如何调用远程接口