iOS动画之贝塞尔曲线和PaintCode
![](https://i-blog.csdnimg.cn/blog_migrate/6d069b6235e412e2b85b52460fb01759.webp?x-image-process=image/format,png)
通常的实现方法是利用CAKeyframeAnimation
的path
属性,这个属性接受一个UIBezierPath
,可以使得layer沿着path移动,代码如下:
// 这个不是飞碟的动画,就是个例子:-D
UIBezierPath *customPath = [UIBezierPath bezierPath];
[customPath moveToPoint:CGPointMake(100,100)];
[customPath addLineToPoint:CGPointMake(200,100)];
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 4.0f;
pathAnimation.path = customPath.CGPath; // 加入贝塞尔路径
pathAnimation.calculationMode = kCAAnimationLinear;
[movingLayer addAnimation:pathAnimation forKey:@"movingAnimation"];
但是问题来了,这条贝塞尔如何制作,难道纯粹意淫,或是让美术标坐标?美术表示鸭梨山大啊,贝塞尔曲线咋标啊?如果像下面这样标条线,你会打死美术吗?
![](https://i-blog.csdnimg.cn/blog_migrate/ffcdefd5256dbf779a020fdb8a9a6416.webp?x-image-process=image/format,png)
所以怎么获取到这条曲线的坐标呢? 这要说到这篇文章的重点了,我使用了PainCode来生成。他的界面是这样的:
![](https://i-blog.csdnimg.cn/blog_migrate/3169af5353a8e1a24441d25830d97d52.webp?x-image-process=image/format,png)
是不是和sketch很像,操作方法也类似,具体使用细节不说了,这里说几个重要的地方:
- 用钢笔工具画线
- 选择infinite display
- 选中一个锚点后,选择make point round
![](https://i-blog.csdnimg.cn/blog_migrate/d3f66c92c79bdc8ea1399a14dceb17d6.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/ca2962ac3bafc1d6b52b83729191f531.webp?x-image-process=image/format,png)
把下面生成的代码放进工程里面,就可以了。需要注意一点,只需要bezier path的代码即可,画图的部分是不需要的,下面这些代码不要了:
[UIColor.blackColor setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];