一、CABasicAnimation
二、CAKeyframeAnimation
三、CATransition
四、CAAnimationGroup
一、CABasicAnimation
// 1.創(chuàng)建動畫對象
CABasicAnimation *anim = [CABasicAnimation animation];
// 2.設(shè)置動畫對象
// keyPath決定了執(zhí)行怎樣的動畫, 調(diào)整哪個屬性來執(zhí)行動畫
// position bounds transform
anim.keyPath = @"position";
// anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, -1, 0)];
// anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
// toValue : 最終變成什么值
// byValue : 增加多少值
anim.byValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
anim.duration = 2.0;
/**讓圖層保持動畫執(zhí)行完畢后的狀態(tài)**/
// 動畫執(zhí)行完畢后不要刪除動畫
anim.removedOnCompletion = NO;
// 保持最新的狀態(tài)
anim.fillMode = kCAFillModeForwards;
// 3.添加動畫
[self.layer addAnimation:anim forKey:nil];
二、CAKeyframeAnimation
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
NSValue *v1 = [NSValue valueWithCGPoint:CGPointZero];
NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(100, 0)];
NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)];
anim.values = @[v1, v2, v3, v4];
// anim.keyTimes = @[@(0.5), @(0.25), @(0.25)];
anim.duration = 2.0;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[self.redView.layer addAnimation:anim forKey:nil];
//路徑
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.duration = 2.0;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
anim.path = path;
CGPathRelease(path);
// 設(shè)置動畫的執(zhí)行節(jié)奏
// kCAMediaTimingFunctionEaseInEaseOut : 一開始比較慢, 中間會加速, 臨近結(jié)束的時候, 會變慢
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.delegate = self;
[self.redView.layer addAnimation:anim forKey:nil];
#pragma mark - 動畫的代理方法
// 動畫開始的時候調(diào)用
- (void)animationDidStart:(CAAnimation *)anim
{
}
// 動畫結(jié)束的時候調(diào)用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
}
例:圖標(biāo)抖動
#define Angle2Radian(angle) ((angle) / 180.0 * M_PI)
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.values = @[@(Angle2Radian(-5)), @(Angle2Radian(5)), @(Angle2Radian(-5))];
anim.duration = 0.25;
// 動畫的重復(fù)執(zhí)行次數(shù)
anim.repeatCount = MAXFLOAT;
// 保持動畫執(zhí)行完畢后的狀態(tài)
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[self.iconView.layer addAnimation:anim forKey:@"shake"];
// 移除
[self.iconView.layer removeAnimationForKey:@"shake"];
三、CATransition
CATransition *anim = [CATransition animation];
anim.type = @"pageCurl";
// anim.subtype = kCATransitionFromRight; //動畫方向
anim.duration = 0.5;
// anim.startProgress = 0.0;
// anim.endProgress = 0.5;
// type
* @"cube" 立方體翻滾效果
* @"moveIn" 新視圖移到舊視圖上面
* @"reveal" 顯露效果(將舊視圖移開,顯示下面的新視圖)
* @"fade" 交叉淡化過渡(不支持過渡方向) (默認(rèn)為此效果)
* @"pageCurl" 向上翻一頁
* @"pageUnCurl" 向下翻一頁
* @"suckEffect" 收縮效果,類似系統(tǒng)最小化窗口時的神奇效果(不支持過渡方向)
* @"rippleEffect" 滴水效果,(不支持過渡方向)
* @"oglFlip" 上下左右翻轉(zhuǎn)效果
* @"rotate" 旋轉(zhuǎn)效果
* @"push"
* @"cameraIrisHollowOpen" 相機(jī)鏡頭打開效果(不支持過渡方向)
* @"cameraIrisHollowClose" 相機(jī)鏡頭關(guān)上效果(不支持過渡方向)
四、CAAnimationGroup
// 1.創(chuàng)建旋轉(zhuǎn)動畫對象
CABasicAnimation *rotate = [CABasicAnimation animation];
rotate.keyPath = @"transform.rotation";
rotate.toValue = @(M_PI);
// 2.創(chuàng)建縮放動畫對象
CABasicAnimation *scale = [CABasicAnimation animation];
scale.keyPath = @"transform.scale";
scale.toValue = @(0.0);
// 3.平移動畫
CABasicAnimation *move = [CABasicAnimation animation];
move.keyPath = @"transform.translation";
move.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
// 4.將所有的動畫添加到動畫組中
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[rotate, scale, move];
group.duration = 2.0;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[self.myvie.layer addAnimation:group forKey:nil];