Core Animation,中文翻譯為核心動(dòng)畫(huà),它是一組非常強(qiáng)大的動(dòng)畫(huà)處理API,使用它能做出非常炫麗的動(dòng)畫(huà)效果,而且往往是事半功倍。也就是說(shuō),使用少量的代碼就可以實(shí)現(xiàn)非常強(qiáng)大的功能。
Core Animation可以用在Mac OS X和iOS平臺(tái)。
Core Animation的動(dòng)畫(huà)執(zhí)行過(guò)程都是在后臺(tái)操作的,不會(huì)阻塞主線程。
要注意的是,Core Animation是直接作用在CALayer上的,并非UIView。
一 、Core Animation結(jié)構(gòu)
其中灰色虛線表示繼承關(guān)系,紅色表示遵守協(xié)議
核心動(dòng)畫(huà)中所有類(lèi)都遵守CAMediaTiming協(xié)議
1.CAAnaimation
是個(gè)抽象類(lèi),不具備動(dòng)畫(huà)效果,必須用它的子類(lèi)才有動(dòng)畫(huà)效果。是所有動(dòng)畫(huà)對(duì)象的父類(lèi),負(fù)責(zé)控制動(dòng)畫(huà)的持續(xù)時(shí)間和速度。
2.CAAnimationGroup和CATransition
是CAAnimation的子類(lèi),有動(dòng)畫(huà)效果。
CAAnimationGroup是個(gè)動(dòng)畫(huà)組,可以同時(shí)進(jìn)行縮放,旋轉(zhuǎn)(同時(shí)進(jìn)行多個(gè)動(dòng)畫(huà))。
CATransition是轉(zhuǎn)場(chǎng)動(dòng)畫(huà),界面之間跳轉(zhuǎn)(切換)都可以用轉(zhuǎn)場(chǎng)動(dòng)畫(huà)。
3.CAPropertyAnimation
是CAAnimation的子類(lèi),也是個(gè)抽象類(lèi),本身不具備動(dòng)畫(huà)效果,只有子類(lèi)才有。要想創(chuàng)建動(dòng)畫(huà)對(duì)象,應(yīng)該使用它的兩個(gè)子類(lèi):CABasicAnimation和CAKeyframeAnimation。
4.CABasicAnimation和CAKeyframeAnimation
CABasicAnimation基本動(dòng)畫(huà),做一些簡(jiǎn)單效果。
CAKeyframeAnimation幀動(dòng)畫(huà),做一些連續(xù)的流暢的動(dòng)畫(huà)。
二、基本使用
以基本動(dòng)畫(huà)為例:
先要有CALayer圖層。
初始化一個(gè)CABasicAnimation對(duì)象,給對(duì)象設(shè)置相關(guān)的屬性。
將基本動(dòng)畫(huà)對(duì)象添加到CALayer對(duì)象中就可以開(kāi)始動(dòng)畫(huà)了。
CALayer *layer = [CALayer layer];
...
CABasicAnimation *animation = [CABasicAnimation animation];
anmation.keyPath = @"transform.scale";
anmation.toValue = @0;
[layer addAnimation:animation forKey:nil];
三、CAAnimation
1、CABasicAnimation(基礎(chǔ)動(dòng)畫(huà))
CAPropertyAnimation的子類(lèi)
屬性解析:
fromValue:keyPath相應(yīng)屬性的初始值
toValue:keyPath相應(yīng)屬性的結(jié)束值
隨著動(dòng)畫(huà)的進(jìn)行,在長(zhǎng)度為duration的持續(xù)時(shí)間內(nèi),keyPath相應(yīng)屬性的值從fromValue漸漸地變?yōu)閠oValue
如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在動(dòng)畫(huà)執(zhí)行完畢后,圖層會(huì)保持顯示動(dòng)畫(huà)執(zhí)行后的狀態(tài)。但在實(shí)質(zhì)上,圖層的屬性值還是動(dòng)畫(huà)執(zhí)行前的初始值,并沒(méi)有真正被改變。
比如,CALayer的position初始值為(0,0),CABasicAnimation的fromValue為(10,10),toValue為(100,100),雖然動(dòng)畫(huà)執(zhí)行完畢后圖層保持在(100,100)這個(gè)位置,實(shí)質(zhì)上圖層的position還是為(0,0)
平移動(dòng)畫(huà)
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//創(chuàng)建layer
CALayer *myLayer=[CALayer layer];
//設(shè)置layer的屬性
myLayer.bounds=CGRectMake(0, 0, 50, 80);
myLayer.backgroundColor=[UIColor yellowColor].CGColor;
myLayer.position=CGPointMake(50, 50);
myLayer.anchorPoint=CGPointMake(0, 0);
myLayer.cornerRadius=20;
//添加layer
[self.view.layer addSublayer:myLayer];
self.myLayer=myLayer;
}
//設(shè)置動(dòng)畫(huà)(基礎(chǔ)動(dòng)畫(huà))
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建核心動(dòng)畫(huà)
// CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>]
CABasicAnimation *anima=[CABasicAnimation animation];
//1.1告訴系統(tǒng)要執(zhí)行什么樣的動(dòng)畫(huà)
anima.keyPath=@"position";
//設(shè)置通過(guò)動(dòng)畫(huà),將layer從哪兒移動(dòng)到哪兒
anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
//1.2設(shè)置動(dòng)畫(huà)執(zhí)行完畢之后不刪除動(dòng)畫(huà)
anima.removedOnCompletion=NO;
//1.3設(shè)置保存動(dòng)畫(huà)的最新?tīng)顟B(tài)
anima.fillMode=kCAFillModeForwards;
anima.delegate=self;
//打印
NSString *str=NSStringFromCGPoint(self.myLayer.position);
NSLog(@"執(zhí)行前:%@",str);
//2.添加核心動(dòng)畫(huà)到layer
[self.myLayer addAnimation:anima forKey:nil];
}
-(void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"開(kāi)始執(zhí)行動(dòng)畫(huà)");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//動(dòng)畫(huà)執(zhí)行完畢,打印執(zhí)行完畢后的position值
NSString *str=NSStringFromCGPoint(self.myLayer.position);
NSLog(@"執(zhí)行后:%@",str);
}
@end
縮放動(dòng)畫(huà)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建動(dòng)畫(huà)
CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"bounds"];
//1.1設(shè)置動(dòng)畫(huà)執(zhí)行時(shí)間
anima.duration=2.0;
//1.2設(shè)置動(dòng)畫(huà)執(zhí)行完畢后不刪除動(dòng)畫(huà)
anima.removedOnCompletion=NO;
//1.3設(shè)置保存動(dòng)畫(huà)的最新?tīng)顟B(tài)
anima.fillMode=kCAFillModeForwards;
//1.4修改屬性,執(zhí)行動(dòng)畫(huà)
anima.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
//2.添加動(dòng)畫(huà)到layer
[self.myLayer addAnimation:anima forKey:nil];
}
旋轉(zhuǎn)動(dòng)畫(huà)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建動(dòng)畫(huà)
CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"transform"];
//1.1設(shè)置動(dòng)畫(huà)執(zhí)行時(shí)間
anima.duration=2.0;
//1.2修改屬性,執(zhí)行動(dòng)畫(huà)
anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
//1.3設(shè)置動(dòng)畫(huà)執(zhí)行完畢后不刪除動(dòng)畫(huà)
anima.removedOnCompletion=NO;
//1.4設(shè)置保存動(dòng)畫(huà)的最新?tīng)顟B(tài)
anima.fillMode=kCAFillModeForwards;
//2.添加動(dòng)畫(huà)到layer
[self.myLayer addAnimation:anima forKey:nil];
}
2、CAKeyframeAnimation(關(guān)鍵幀動(dòng)畫(huà))
關(guān)鍵幀動(dòng)畫(huà),也是CAPropertyAnimation的子類(lèi),與CABasicAnimation的區(qū)別是:
CABasicAnimation只能從一個(gè)數(shù)值(fromValue)變到另一個(gè)數(shù)值(toValue),而CAKeyframeAnimation會(huì)使用一個(gè)NSArray保存這些數(shù)值
CABasicAnimation可看做是只有2個(gè)關(guān)鍵幀的CAKeyframeAnimation
修改位置
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1.創(chuàng)建關(guān)鍵幀動(dòng)畫(huà)
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.duration = 2;
// 2.設(shè)置屬性
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(300, 200)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(300, 500)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(100, 500)];
anim.values = @[value1, value2, value3, value4, value1];
// 3.添加到layer上
[self.magLayer addAnimation:anim forKey:nil];
}
沿著路徑走
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1.創(chuàng)建
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.duration = 2;
// 2.設(shè)置屬性
anim.path = [UIBezierPath bezierPathWithOvalInRect:self.view.bounds].CGPath;
// 3.添加
[self.magLayer addAnimation:anim forKey:nil];
}
圖標(biāo)抖動(dòng)效果
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1.創(chuàng)建關(guān)鍵幀動(dòng)畫(huà)
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
// 2.設(shè)置屬性
anim.values = @[@(-M_PI_4 * 0.2),@(M_PI_4 * 0.2), @(-M_PI_4 * 0.2)];
anim.repeatCount = CGFLOAT_MAX;
// 3.添加
[self.magLayer addAnimation:anim forKey:nil];
}
改變大小
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 修改大小
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"bounds.size"];
anim.values = @[
[NSValue valueWithCGSize:CGSizeMake(200, 200)],
[NSValue valueWithCGSize:CGSizeMake(80, 80)],
];
anim.repeatCount = CGFLOAT_MAX;
[self.magLayer addAnimation:anim forKey:nil];
}
3、CAAnimationGroup(動(dòng)畫(huà)組)
動(dòng)畫(huà)組,是CAAnimation的子類(lèi),可以保存一組動(dòng)畫(huà)對(duì)象,將CAAnimationGroup對(duì)象加入層后,組中所有動(dòng)畫(huà)對(duì)象可以同時(shí)并發(fā)運(yùn)行。
默認(rèn)情況下,一組動(dòng)畫(huà)對(duì)象是同時(shí)運(yùn)行的,也可以通過(guò)設(shè)置動(dòng)畫(huà)對(duì)象的beginTime屬性來(lái)更改動(dòng)畫(huà)的開(kāi)始時(shí)間。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1.創(chuàng)建組動(dòng)畫(huà)對(duì)象
CAAnimationGroup *group = [CAAnimationGroup animation];
// 2.設(shè)置動(dòng)畫(huà)屬性
group.animations = @[
[self anim1],
[self anim2],
[self anim3],
];
group.duration = 2;
group.repeatCount = CGFLOAT_MAX;
// 3.添加到layer上
[self.magLayer addAnimation:group forKey:nil];
}
4、CATransition(轉(zhuǎn)場(chǎng)動(dòng)畫(huà))
CATransition是CAAnimation的子類(lèi),用于做轉(zhuǎn)場(chǎng)動(dòng)畫(huà),能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭?dòng)畫(huà)效果。iOS比Mac OS X的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)效果少一點(diǎn)。
UINavigationController就是通過(guò)CATransition實(shí)現(xiàn)了將控制器的視圖推入屏幕的動(dòng)畫(huà)效果。
#pragma mark - 2.輕掃觸發(fā)
- (void)swipeAction:(UISwipeGestureRecognizer *)recognizer {
// MARK: - 轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
// 1.創(chuàng)建動(dòng)畫(huà)對(duì)象
CATransition *anim = [CATransition animation];
// 兩個(gè)屬性 type[動(dòng)畫(huà)的類(lèi)型], subType[子類(lèi)型]
// 2.設(shè)置動(dòng)畫(huà)屬性
anim.type = kCATransitionReveal;
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右");
anim.subtype = kCATransitionFromLeft;
} else {
NSLog(@"向左");
anim.subtype = kCATransitionFromRight;
}
UIImageView *imgView = (UIImageView *)recognizer.view;
imgView.image = [UIImage imageNamed:imgName];
// 3.添加動(dòng)畫(huà)
[imgView.layer addAnimation:anim forKey:nil];
}