時(shí)間函數(shù):動(dòng)畫總會(huì)在設(shè)定的時(shí)間長度內(nèi)執(zhí)行完乞巧,在這段時(shí)間內(nèi)未荒,動(dòng)畫的變化速度有多快,主要決定于時(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ù)
- 緩入
- 緩出
- 慢入慢出
- 默認(rèn)
系統(tǒng)提供的設(shè)置時(shí)間函數(shù)的方法
[CAMediaTimingFunction functionWithControlPoints::::]
里面的四個(gè)參數(shù)范圍值都是0.0 -1.0
之間,這些數(shù)值的由來可以看曲線圖起始點(diǎn)和終點(diǎn)對(duì)應(yīng)的切線抖仅。
思路:拆分動(dòng)畫
- 1.把基礎(chǔ)圖形設(shè)置為兩個(gè)連著的半圓弧
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 那么接下來我們要實(shí)現(xiàn)讓它動(dòng)起來:實(shí)現(xiàn)紅色蟲運(yùn)動(dòng)的前半部分如下圖
//第一階段蟲子的拉伸
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = [NSNumber numberWithFloat:0.0];
animation.toValue = [NSNumber numberWithFloat:0.5];
//從0到0.5表示動(dòng)畫的過程從無到一個(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)紅色蟲前半部收縮的效果如下圖
//第一階段蟲子的縮小
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
的效果完成之后才開始3
的效果,剛好銜接上去就湊成了一個(gè)完整的動(dòng)畫稚茅。然后把這兩個(gè)放到一個(gè)動(dòng)畫組里面試試效果.
- 4 實(shí)現(xiàn)前半部分動(dòng)畫完整銜接如下圖
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)紅色蟲后半部分的拉伸和收縮如下圖
//第二階段蟲子的拉伸
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;
//第二階段蟲子的縮小
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)畫一起放入動(dòng)畫組里面實(shí)現(xiàn)效果如下圖
- 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)畫組里面運(yùn)行就實(shí)現(xiàn)了紅色的效果
![基本動(dòng)畫分解7.gif](http://upload-images.jianshu.io/upload_images/1851080-6b2a75e429e3baf3.gif?imageMogr2/auto-orient/strip)
同理纸淮,可實(shí)現(xiàn)黃色和綠色的加入。