当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS开发教程之扇形动画的实现

iOS开发教程之扇形动画的实现

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言

最近比较闲,正好利用这段时间把现在项目用的东西封装一下,方便以后复用,当然好的东西还是要分享。一起学习,一起进步。

看图片,很显然这是一个扇形图,相信大家对做扇形图得心应手,可能对做扇形动画有一定难度,不急,下面给出代码和思路。

针对项目用的扇形动画,在这个基础上我做了一下封装。

核心代码如下:

-(instancetype)initwithcenter:(cgpoint)center radius:(cgfloat)radius bgcolor:(uicolor *)bgcolor repeatcount:(nsinteger)repeatcount {
 if (self = [super init]) {

  //设置self的frame和center
  self.backgroundcolor = bgcolor;
  self.frame = cgrectmake(0, 0, radius * 2, radius * 2);
  self.center = center;

  _repeatcount = repeatcount;
  //特别注意:贝塞尔曲线的radius必须为self高度的四分之一,cashapelayer的线宽必须为self高度的二分之一
  uibezierpath *path = [uibezierpath bezierpathwitharccenter:cgpointmake(radius, radius) radius:radius / 2 startangle:-m_pi/2 endangle:m_pi *3 / 2 clockwise:yes];

  cashapelayer *masklayer = [cashapelayer layer];
  masklayer.path = path.cgpath;
  masklayer.fillcolor = [uicolor clearcolor].cgcolor;
  masklayer.strokecolor = bgcolor.cgcolor;
  masklayer.linewidth = radius; //等于半径的2倍,以圆的边缘为中心,向圆内部伸展一个半径,向外伸展一个半径,所以看上去以为圆的半径是self高度的一半。

  self.layer.mask = masklayer;
  _masklayer = masklayer;
 }

 return self;
}
-(void)startanimaiton {
 //开始执行扇形动画
 cabasicanimation *strokeendani = [cabasicanimation animationwithkeypath:@"strokeend"];
 strokeendani.fromvalue = @0;
 strokeendani.tovalue = @1;
 strokeendani.duration = 1;
 //重复次数
 strokeendani.repeatcount = _repeatcount;

 [_masklayer addanimation:strokeendani forkey:@"ani"];
}

思路

可以让fillcolor 为clearcolor 让linewidth充满整个圆,然后让strokeend执行动画,从而实现扇形动画。

下载地址:源码下载 |

调用方法很简单:直接看api

/**
初始化对象
@param center 中心
@param radius self宽度的一半
@param bgcolor 背景色
@param repeatcount 动画重复次数
@return self
*/
-(instancetype)initwithcenter:(cgpoint)center radius:(cgfloat)radius bgcolor:(uicolor *)bgcolor repeatcount:(nsinteger)repeatcount;
-(void)startanimaiton;
-(void)puaseanimation;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网