Core Animation,中文翻譯為核心動(dòng)畫弄唧,它是一組非常強(qiáng)大的動(dòng)畫處理API适肠,使用它能做出非常炫麗的動(dòng)畫效果,而且往往是事半功倍候引。也就是說侯养,使用少量的代碼就可以實(shí)現(xiàn)非常強(qiáng)大的功能。
Core Animation可以用在Mac OS X和iOS平臺(tái)澄干。
Core Animation的動(dòng)畫執(zhí)行過程都是在后臺(tái)操作的逛揩,不會(huì)阻塞主線程。
要注意的是麸俘,Core Animation是直接作用在CALayer上的辩稽,并非UIView。
一 从媚、Core Animation結(jié)構(gòu)
其中灰色虛線表示繼承關(guān)系逞泄,紅色表示遵守協(xié)議。
核心動(dòng)畫中所有類都遵守CAMediaTiming協(xié)議拜效。
1.CAAnaimation
是個(gè)抽象類喷众,不具備動(dòng)畫效果,必須用它的子類才有動(dòng)畫效果紧憾。是所有動(dòng)畫對(duì)象的父類到千,負(fù)責(zé)控制動(dòng)畫的持續(xù)時(shí)間和速度。
2.CAAnimationGroup和CATransition
是CAAnimation的子類,有動(dòng)畫效果稻励。
CAAnimationGroup是個(gè)動(dòng)畫組父阻,可以同時(shí)進(jìn)行縮放,旋轉(zhuǎn)(同時(shí)進(jìn)行多個(gè)動(dòng)畫)望抽。
CATransition是轉(zhuǎn)場(chǎng)動(dòng)畫加矛,界面之間跳轉(zhuǎn)(切換)都可以用轉(zhuǎn)場(chǎng)動(dòng)畫。
3.CAPropertyAnimation
是CAAnimation的子類煤篙,也是個(gè)抽象類斟览,本身不具備動(dòng)畫效果,只有子類才有辑奈。要想創(chuàng)建動(dòng)畫對(duì)象苛茂,應(yīng)該使用它的兩個(gè)子類:CABasicAnimation和CAKeyframeAnimation已烤。
4.CABasicAnimation和CAKeyframeAnimation
CABasicAnimation基本動(dòng)畫,做一些簡(jiǎn)單效果妓羊。
CAKeyframeAnimation幀動(dòng)畫胯究,做一些連續(xù)的流暢的動(dòng)畫。
二躁绸、基本使用
以基本動(dòng)畫為例:
先要有CALayer圖層裕循。
初始化一個(gè)CABasicAnimation對(duì)象,給對(duì)象設(shè)置相關(guān)的屬性净刮。
將基本動(dòng)畫對(duì)象添加到CALayer對(duì)象中就可以開始動(dòng)畫了剥哑。
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)畫)
CAPropertyAnimation的子類
屬性解析:
fromValue:keyPath相應(yīng)屬性的初始值
toValue:keyPath相應(yīng)屬性的結(jié)束值
隨著動(dòng)畫的進(jìn)行株婴,在長(zhǎng)度為duration的持續(xù)時(shí)間內(nèi),keyPath相應(yīng)屬性的值從fromValue漸漸地變?yōu)閠oValue
如果fillMode=kCAFillModeForwards和removedOnComletion=NO暑认,那么在動(dòng)畫執(zhí)行完畢后困介,圖層會(huì)保持顯示動(dòng)畫執(zhí)行后的狀態(tài)。但在實(shí)質(zhì)上穷吮,圖層的屬性值還是動(dòng)畫執(zhí)行前的初始值逻翁,并沒有真正被改變。
比如捡鱼,CALayer的position初始值為(0,0),CABasicAnimation的fromValue為(10,10)酷愧,toValue為(100,100)驾诈,雖然動(dòng)畫執(zhí)行完畢后圖層保持在(100,100)這個(gè)位置,實(shí)質(zhì)上圖層的position還是為(0,0)
平移動(dòng)畫
#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)畫(基礎(chǔ)動(dòng)畫)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建核心動(dòng)畫
// CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>]
CABasicAnimation *anima=[CABasicAnimation animation];
//1.1告訴系統(tǒng)要執(zhí)行什么樣的動(dòng)畫
anima.keyPath=@"position";
//設(shè)置通過動(dòng)畫溶浴,將layer從哪兒移動(dòng)到哪兒
anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
//1.2設(shè)置動(dòng)畫執(zhí)行完畢之后不刪除動(dòng)畫
anima.removedOnCompletion=NO;
//1.3設(shè)置保存動(dòng)畫的最新狀態(tài)
anima.fillMode=kCAFillModeForwards;
anima.delegate=self;
//打印
NSString *str=NSStringFromCGPoint(self.myLayer.position);
NSLog(@"執(zhí)行前:%@",str);
//2.添加核心動(dòng)畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
-(void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"開始執(zhí)行動(dòng)畫");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//動(dòng)畫執(zhí)行完畢乍迄,打印執(zhí)行完畢后的position值
NSString *str=NSStringFromCGPoint(self.myLayer.position);
NSLog(@"執(zhí)行后:%@",str);
}
@end
縮放動(dòng)畫
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建動(dòng)畫
CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"bounds"];
//1.1設(shè)置動(dòng)畫執(zhí)行時(shí)間
anima.duration=2.0;
//1.2設(shè)置動(dòng)畫執(zhí)行完畢后不刪除動(dòng)畫
anima.removedOnCompletion=NO;
//1.3設(shè)置保存動(dòng)畫的最新狀態(tài)
anima.fillMode=kCAFillModeForwards;
//1.4修改屬性,執(zhí)行動(dòng)畫
anima.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
//2.添加動(dòng)畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
旋轉(zhuǎn)動(dòng)畫
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建動(dòng)畫
CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"transform"];
//1.1設(shè)置動(dòng)畫執(zhí)行時(shí)間
anima.duration=2.0;
//1.2修改屬性士败,執(zhí)行動(dòng)畫
anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
//1.3設(shè)置動(dòng)畫執(zhí)行完畢后不刪除動(dòng)畫
anima.removedOnCompletion=NO;
//1.4設(shè)置保存動(dòng)畫的最新狀態(tài)
anima.fillMode=kCAFillModeForwards;
//2.添加動(dòng)畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
2闯两、CAKeyframeAnimation(關(guān)鍵幀動(dòng)畫)
關(guān)鍵幀動(dòng)畫,也是CAPropertyAnimation的子類谅将,與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)畫
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)畫
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)畫組)
動(dòng)畫組,是CAAnimation的子類饥臂,可以保存一組動(dòng)畫對(duì)象逊躁,將CAAnimationGroup對(duì)象加入層后,組中所有動(dòng)畫對(duì)象可以同時(shí)并發(fā)運(yùn)行隅熙。
默認(rèn)情況下,一組動(dòng)畫對(duì)象是同時(shí)運(yùn)行的稽煤,也可以通過設(shè)置動(dòng)畫對(duì)象的beginTime屬性來更改動(dòng)畫的開始時(shí)間核芽。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1.創(chuàng)建組動(dòng)畫對(duì)象
CAAnimationGroup *group = [CAAnimationGroup animation];
// 2.設(shè)置動(dòng)畫屬性
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)畫)
CATransition是CAAnimation的子類酵熙,用于做轉(zhuǎn)場(chǎng)動(dòng)畫轧简,能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭?dòng)畫效果。iOS比Mac OS X的轉(zhuǎn)場(chǎng)動(dòng)畫效果少一點(diǎn)匾二。
UINavigationController就是通過CATransition實(shí)現(xiàn)了將控制器的視圖推入屏幕的動(dòng)畫效果吉懊。
#pragma mark - 2.輕掃觸發(fā)
- (void)swipeAction:(UISwipeGestureRecognizer *)recognizer {
// MARK: - 轉(zhuǎn)場(chǎng)動(dòng)畫
// 1.創(chuàng)建動(dòng)畫對(duì)象
CATransition *anim = [CATransition animation];
// 兩個(gè)屬性 type[動(dòng)畫的類型], subType[子類型]
// 2.設(shè)置動(dòng)畫屬性
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)畫
[imgView.layer addAnimation:anim forKey:nil];
}