Core Animation,中文翻譯為核心動畫吨凑,它是一組非常強大的動畫處理API,使用它能做出非常炫麗的動畫效果户辱,而且往往是事半功倍鸵钝。也就是說,使用少量的代碼就可以實現(xiàn)非常強大的功能庐镐。
Core Animation可以用在Mac OS X和iOS平臺恩商。
Core Animation的動畫執(zhí)行過程都是在后臺操作的,不會阻塞主線程必逆。
要注意的是怠堪,Core Animation是直接作用在CALayer上的揽乱,并非UIView。
一 粟矿、Core Animation結(jié)構(gòu)
其中灰色虛線表示繼承關(guān)系凰棉,紅色表示遵守協(xié)議
核心動畫中所有類都遵守CAMediaTiming協(xié)議
1.CAAnaimation
是個抽象類,不具備動畫效果陌粹,必須用它的子類才有動畫效果撒犀。是所有動畫對象的父類,負(fù)責(zé)控制動畫的持續(xù)時間和速度掏秩。
2.CAAnimationGroup和CATransition
是CAAnimation的子類,有動畫效果或舞。
CAAnimationGroup是個動畫組,可以同時進(jìn)行縮放蒙幻,旋轉(zhuǎn)(同時進(jìn)行多個動畫)映凳。
CATransition是轉(zhuǎn)場動畫,界面之間跳轉(zhuǎn)(切換)都可以用轉(zhuǎn)場動畫邮破。
3.CAPropertyAnimation
是CAAnimation的子類魏宽,也是個抽象類,本身不具備動畫效果决乎,只有子類才有队询。要想創(chuàng)建動畫對象,應(yīng)該使用它的兩個子類:CABasicAnimation和CAKeyframeAnimation构诚。
4.CABasicAnimation和CAKeyframeAnimation
CABasicAnimation基本動畫蚌斩,做一些簡單效果。
CAKeyframeAnimation幀動畫范嘱,做一些連續(xù)的流暢的動畫送膳。
二、基本使用
以基本動畫為例:
先要有CALayer圖層丑蛤。
初始化一個CABasicAnimation對象叠聋,給對象設(shè)置相關(guān)的屬性。
將基本動畫對象添加到CALayer對象中就可以開始動畫了受裹。
CALayer *layer = [CALayer layer];
...
CABasicAnimation *animation = [CABasicAnimation animation];
anmation.keyPath = @"transform.scale";
anmation.toValue = @0;
[layer addAnimation:animation forKey:nil];
三碌补、CAAnimation
1、CABasicAnimation(基礎(chǔ)動畫)
CAPropertyAnimation的子類
屬性解析:
fromValue:keyPath相應(yīng)屬性的初始值
toValue:keyPath相應(yīng)屬性的結(jié)束值
隨著動畫的進(jìn)行棉饶,在長度為duration的持續(xù)時間內(nèi)厦章,keyPath相應(yīng)屬性的值從fromValue漸漸地變?yōu)閠oValue
如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在動畫執(zhí)行完畢后照藻,圖層會保持顯示動畫執(zhí)行后的狀態(tài)袜啃。但在實質(zhì)上,圖層的屬性值還是動畫執(zhí)行前的初始值幸缕,并沒有真正被改變群发。
比如晰韵,CALayer的position初始值為(0,0),CABasicAnimation的fromValue為(10,10)熟妓,toValue為(100,100)雪猪,雖然動畫執(zhí)行完畢后圖層保持在(100,100)這個位置,實質(zhì)上圖層的position還是為(0,0)
平移動畫
#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è)置動畫(基礎(chǔ)動畫)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建核心動畫
// CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>]
CABasicAnimation *anima=[CABasicAnimation animation];
//1.1告訴系統(tǒng)要執(zhí)行什么樣的動畫
anima.keyPath=@"position";
//設(shè)置通過動畫滑蚯,將layer從哪兒移動到哪兒
anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
//1.2設(shè)置動畫執(zhí)行完畢之后不刪除動畫
anima.removedOnCompletion=NO;
//1.3設(shè)置保存動畫的最新狀態(tài)
anima.fillMode=kCAFillModeForwards;
anima.delegate=self;
//打印
NSString *str=NSStringFromCGPoint(self.myLayer.position);
NSLog(@"執(zhí)行前:%@",str);
//2.添加核心動畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
-(void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"開始執(zhí)行動畫");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//動畫執(zhí)行完畢浪蹂,打印執(zhí)行完畢后的position值
NSString *str=NSStringFromCGPoint(self.myLayer.position);
NSLog(@"執(zhí)行后:%@",str);
}
@end
縮放動畫
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建動畫
CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"bounds"];
//1.1設(shè)置動畫執(zhí)行時間
anima.duration=2.0;
//1.2設(shè)置動畫執(zhí)行完畢后不刪除動畫
anima.removedOnCompletion=NO;
//1.3設(shè)置保存動畫的最新狀態(tài)
anima.fillMode=kCAFillModeForwards;
//1.4修改屬性,執(zhí)行動畫
anima.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
//2.添加動畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
旋轉(zhuǎn)動畫
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建動畫
CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"transform"];
//1.1設(shè)置動畫執(zhí)行時間
anima.duration=2.0;
//1.2修改屬性告材,執(zhí)行動畫
anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
//1.3設(shè)置動畫執(zhí)行完畢后不刪除動畫
anima.removedOnCompletion=NO;
//1.4設(shè)置保存動畫的最新狀態(tài)
anima.fillMode=kCAFillModeForwards;
//2.添加動畫到layer
[self.myLayer addAnimation:anima forKey:nil];
}
2坤次、CAKeyframeAnimation(關(guān)鍵幀動畫)
關(guān)鍵幀動畫,也是CAPropertyAnimation的子類斥赋,與CABasicAnimation的區(qū)別是:
CABasicAnimation只能從一個數(shù)值(fromValue)變到另一個數(shù)值(toValue)缰猴,而CAKeyframeAnimation會使用一個NSArray保存這些數(shù)值
CABasicAnimation可看做是只有2個關(guān)鍵幀的CAKeyframeAnimation
修改位置
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1.創(chuàng)建關(guān)鍵幀動畫
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)抖動效果
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1.創(chuàng)建關(guān)鍵幀動畫
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(動畫組)
動畫組,是CAAnimation的子類疤剑,可以保存一組動畫對象滑绒,將CAAnimationGroup對象加入層后,組中所有動畫對象可以同時并發(fā)運行隘膘。
默認(rèn)情況下,一組動畫對象是同時運行的疑故,也可以通過設(shè)置動畫對象的beginTime屬性來更改動畫的開始時間。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1.創(chuàng)建組動畫對象
CAAnimationGroup *group = [CAAnimationGroup animation];
// 2.設(shè)置動畫屬性
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)場動畫)
CATransition是CAAnimation的子類纵势,用于做轉(zhuǎn)場動畫,能夠為層提供移出屏幕和移入屏幕的動畫效果管钳。iOS比Mac OS X的轉(zhuǎn)場動畫效果少一點钦铁。
UINavigationController就是通過CATransition實現(xiàn)了將控制器的視圖推入屏幕的動畫效果。
#pragma mark - 2.輕掃觸發(fā)
- (void)swipeAction:(UISwipeGestureRecognizer *)recognizer {
// MARK: - 轉(zhuǎn)場動畫
// 1.創(chuàng)建動畫對象
CATransition *anim = [CATransition animation];
// 兩個屬性 type[動畫的類型], subType[子類型]
// 2.設(shè)置動畫屬性
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.添加動畫
[imgView.layer addAnimation:anim forKey:nil];
}