時(shí)間函數(shù):動(dòng)畫(huà)總會(huì)在設(shè)定的時(shí)間長(zhǎng)度內(nèi)執(zhí)行完,在這段時(shí)間內(nèi),動(dòng)畫(huà)的變化速度有多快,主要決定于時(shí)間函數(shù)
CA_EXTERN NSString * const kCAMediaTimingFunctionLinear //線性函數(shù)
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseIn //緩入
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseOut //緩出
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseInEaseOut //慢入慢出
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionDefault //默認(rèn)
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
- 線性函數(shù)

Paste_Image.png
- 緩入

Paste_Image.png
- 緩出

Paste_Image.png
- 慢入慢出

Paste_Image.png
- 默認(rèn)

Paste_Image.png
系統(tǒng)提供的設(shè)置時(shí)間函數(shù)的方法
[CAMediaTimingFunction functionWithControlPoints::::]里面的四個(gè)參數(shù)范圍值都是0.0 -1.0之間,這些數(shù)值的由來(lái)可以看曲線圖起始點(diǎn)和終點(diǎn)對(duì)應(yīng)的切線。

基本動(dòng)畫(huà).gif
思路:拆分動(dòng)畫(huà)
- 1.把基礎(chǔ)圖形設(shè)置為兩個(gè)連著的半圓弧

Paste_Image.png
CAShapeLayer * first = [CAShapeLayer layer];
first.path = [self wormRunLongPath];
first.lineWidth = 10;
first.lineCap = kCALineCapRound;
first.lineJoin = kCALineJoinRound;
first.strokeColor = [UIColor redColor].CGColor;
first.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:first];
- (CGPathRef)wormRunLongPath {
//中間點(diǎn)
CGPoint center = CGPointMake(220, 300/2);
CGFloat radius = 30;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(center.x - radius , center.y) radius:radius startAngle:M_PI endAngle:2 * M_PI clockwise:YES];
[path addArcWithCenter:CGPointMake(center.x + radius, center.y) radius:radius startAngle:M_PI endAngle:2 * M_PI clockwise:YES];
return path.CGPath;
}
運(yùn)行,就得到了上面兩個(gè)半圓的效果。
- 2 那么接下來(lái)我們要實(shí)現(xiàn)讓它動(dòng)起來(lái):實(shí)現(xiàn)紅色蟲(chóng)運(yùn)動(dòng)的前半部分如下圖

基本動(dòng)畫(huà)分解1.gif
//第一階段蟲(chóng)子的拉伸
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = [NSNumber numberWithFloat:0.0];
animation.toValue = [NSNumber numberWithFloat:0.5];
//從0到0.5表示動(dòng)畫(huà)的過(guò)程從無(wú)到一個(gè)半圓
animation.duration = 0.75;
animation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.42 :0.0 :1.0 :0.55];
animation.removedOnCompletion = false;
animation.fillMode = kCAFillModeForwards;
運(yùn)行,OK.
- 3 實(shí)現(xiàn)紅色蟲(chóng)前半部收縮的效果如下圖

基本動(dòng)畫(huà)分解2.gif
//第一階段蟲(chóng)子的縮小
CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
animation1.fromValue = [NSNumber numberWithFloat:0.0];
animation1.toValue = [NSNumber numberWithFloat:0.5];
animation1.duration = 0.45;
animation1.beginTime = 0.75;
animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation1.fillMode = kCAFillModeForwards;
稍微注意一下我們就發(fā)現(xiàn)其實(shí)只是keyPath的區(qū)別,一個(gè)是start,一個(gè)是end,同事我們?cè)O(shè)置beginTime表示的是在2的效果完成之后才開(kāi)始3的效果,剛好銜接上去就湊成了一個(gè)完整的動(dòng)畫(huà)。然后把這兩個(gè)放到一個(gè)動(dòng)畫(huà)組里面試試效果.
- 4 實(shí)現(xiàn)前半部分動(dòng)畫(huà)完整銜接如下圖

基本動(dòng)畫(huà)分解3.gif
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:animation,animation1,nil];
group.repeatCount = HUGE_VALF;
group.duration = 0.75 * 2;
[self.firstWormShapeLayer addAnimation:group forKey:@"one"];
- 5.同理實(shí)現(xiàn)紅色蟲(chóng)后半部分的拉伸和收縮如下圖

基本動(dòng)畫(huà)分解4.gif

基本動(dòng)畫(huà)分解5.gif
//第二階段蟲(chóng)子的拉伸
CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation2.fromValue = [NSNumber numberWithFloat:0.5];
animation2.toValue = [NSNumber numberWithFloat:1.0];
animation2.duration = 0.75;
animation2.beginTime = 1.2;
animation2.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.42 :0.0 :1.0 :0.55];
animation2.fillMode = kCAFillModeForwards;
//第二階段蟲(chóng)子的縮小
CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
animation3.fromValue = [NSNumber numberWithFloat:0.5];
animation3.toValue = [NSNumber numberWithFloat:1];
animation3.duration = 0.45;
animation3.beginTime = 0.75 + 1.2;
animation3.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation3.fillMode = kCAFillModeForwards;
-
6 把上面四個(gè)動(dòng)畫(huà)一起放入動(dòng)畫(huà)組里面實(shí)現(xiàn)效果如下圖
基本動(dòng)畫(huà)分解6.gif - 7 然后分析一下我們發(fā)現(xiàn)跟效果圖就差了一個(gè)關(guān)于
x的位移//實(shí)現(xiàn)持續(xù)移動(dòng)的效果:第一階段平移 CABasicAnimation *xTranslationAm = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; xTranslationAm.toValue = [NSNumber numberWithFloat: (60 / -1.0)]; xTranslationAm.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; xTranslationAm.duration = 1.18; xTranslationAm.fillMode = kCAFillModeForwards; //實(shí)現(xiàn)持續(xù)移動(dòng)的效果:第二階段平移 CABasicAnimation *xTranslationAm2 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; xTranslationAm2.toValue = [NSNumber numberWithFloat: (60 / -1.0) * 2]; xTranslationAm2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; xTranslationAm2.duration = 1.18; xTranslationAm2.beginTime = 1.20; xTranslationAm2.fillMode = kCAFillModeForwards;
把位移加入到動(dòng)畫(huà)組里面運(yùn)行就實(shí)現(xiàn)了紅色的效果

同理,可實(shí)現(xiàn)黃色和綠色的加入。
