項目中遇到幾個動畫效果,需要一個UIImageView
沿著一個特定的路徑移動,美術的設計是這樣的:
通常的實現方法是利用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"];
但是問題來了,這條貝塞爾如何制作,難道純粹意淫,或是讓美術標坐標?美術表示鴨梨山大啊,貝塞爾曲線咋標啊?如果像下面這樣標條線,你會打死美術嗎?
所以怎么獲取到這條曲線的坐標呢? 這要說到這篇文章的重點了,我使用了PainCode來生成。他的界面是這樣的:
是不是和sketch很像,操作方法也類似,具體使用細節不說了,這里說幾個重要的地方:
- 用鋼筆工具畫線
- 選擇infinite display
- 選中一個錨點后,選擇make point round
把下面生成的代碼放進工程里面,就可以了。需要注意一點,只需要bezier path的代碼即可,畫圖的部分是不需要的,下面這些代碼不要了:
[UIColor.blackColor setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];