iOS核心動(dòng)畫Core Animation分為幾類:基礎(chǔ)動(dòng)畫、關(guān)鍵幀動(dòng)畫、動(dòng)畫組、轉(zhuǎn)場(chǎng)動(dòng)畫。關(guān)系大致如下圖:
CAAnimation:核心動(dòng)畫的基礎(chǔ)類,不能直接使用,負(fù)責(zé)動(dòng)畫運(yùn)行時(shí)間、速度的控制,本身實(shí)現(xiàn)了CAMediaTiming協(xié)議。
CAAnimationGroup:動(dòng)畫組,動(dòng)畫組是一種組合模式設(shè)計(jì),可以通過動(dòng)畫組來進(jìn)行所有動(dòng)畫行為的統(tǒng)一控制,組中所有動(dòng)畫效果可以并發(fā)執(zhí)行。
CAPropertyAnimation:屬性動(dòng)畫的基類(通過屬性進(jìn)行動(dòng)畫設(shè)置,注意是可動(dòng)畫屬性),不能直接使用。
CATransition:轉(zhuǎn)場(chǎng)動(dòng)畫,主要通過濾鏡進(jìn)行動(dòng)畫效果設(shè)置。
CABasicAnimation:基礎(chǔ)動(dòng)畫,通過屬性修改進(jìn)行動(dòng)畫參數(shù)控制,只有初始狀態(tài)和結(jié)束狀態(tài)。
CAKeyframeAnimation:關(guān)鍵幀動(dòng)畫,通過屬性進(jìn)行動(dòng)畫參數(shù)控制,可以有多個(gè)狀態(tài)控制。
CASpringAnimation:彈簧動(dòng)畫,通過屬性進(jìn)行動(dòng)畫參數(shù)控制,可以有多個(gè)狀態(tài)控制。但是9.0才有的屬性。
基礎(chǔ)動(dòng)畫、關(guān)鍵幀動(dòng)畫都屬于屬性動(dòng)畫,就是通過修改屬性值產(chǎn)生動(dòng)畫效果,開發(fā)人員只需要設(shè)置初始值和結(jié)束值,中間的過程動(dòng)畫(又叫“補(bǔ)間動(dòng)畫”)由系統(tǒng)自動(dòng)計(jì)算產(chǎn)生。和基礎(chǔ)動(dòng)畫不同的是關(guān)鍵幀動(dòng)畫可以設(shè)置多個(gè)屬性值,每兩個(gè)屬性中間的補(bǔ)間動(dòng)畫由系統(tǒng)自動(dòng)完成。
基礎(chǔ)動(dòng)畫
基礎(chǔ)動(dòng)畫可以滿足大部分的開發(fā)需求,UIView封裝了一些方法提供使用,如果不使用封裝方法,創(chuàng)建動(dòng)畫一般分為以下幾步:
?1、初始化動(dòng)畫,并指定動(dòng)畫屬性
?2、設(shè)置動(dòng)畫屬性
?3、給圖層添加動(dòng)畫
下面移動(dòng)動(dòng)畫作為實(shí)例,點(diǎn)擊屏幕,物體移動(dòng)到點(diǎn)擊位置。
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property (nonatomic, strong) CALayer *animationCALayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.animationCALayer = [[CALayer alloc] init];
self.animationCALayer.bounds = CGRectMake(0, 0, 40, 40);
self.animationCALayer.position = CGPointMake(50, 150);
self.animationCALayer.contents = (id)[UIImage imageNamed:@"vehicleResource.png"].CGImage;
[self.view.layer addSublayer:self.animationCALayer];
}
#pragma mark - 點(diǎn)擊事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = touches.anyObject;
CGPoint location = [touch locationInView:self.view];
[self animationWhitPoint:location];
}
#pragma mark - 移動(dòng)動(dòng)畫
- (void)animationWhitPoint:(CGPoint) location {
// 1、創(chuàng)建動(dòng)畫并指定動(dòng)畫屬性
CABasicAnimation *myBasicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
// 2、設(shè)置動(dòng)畫屬性
// myBasicAnimation.fromValue = [NSNumber numberWithInteger:50];
myBasicAnimation.toValue = [NSValue valueWithCGPoint:location];
// 設(shè)置動(dòng)畫其他屬性 事件 重復(fù)次數(shù) 運(yùn)行一次是否移除動(dòng)畫
myBasicAnimation.duration = 5;
// myBasicAnimation.repeatCount = 2;
// myBasicAnimation.removedOnCompletion = NO;
// 3、添加動(dòng)畫到圖層,注意key相當(dāng)于給動(dòng)畫進(jìn)行命名,以后獲得該動(dòng)畫時(shí)可以使用此名稱獲取
[self.animationCALayer addAnimation:myBasicAnimation forKey:@"KCBasicAnimation_Translation"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
基本效果已經(jīng)達(dá)到,但是物體動(dòng)畫效果結(jié)束后又回到了原來的位置,然而使用UIView封裝的方法就不會(huì)出現(xiàn)這種情況。應(yīng)該怎么解決?
?圖層動(dòng)畫的本質(zhì)就是將圖層內(nèi)部的內(nèi)容轉(zhuǎn)化為位圖經(jīng)硬件操作形成一種動(dòng)畫效果,其實(shí)圖層本身并沒有任何的變化。上面的動(dòng)畫中圖層并沒有因?yàn)閯?dòng)畫效果而改變它的位置(對(duì)于縮放動(dòng)畫其大小也是不會(huì)改變的),所以動(dòng)畫完成之后圖層還是在原來的顯示位置沒有任何變化,如果這個(gè)圖層在一個(gè)UIView中你會(huì)發(fā)現(xiàn)在UIView移動(dòng)過程中你要觸發(fā)UIView的點(diǎn)擊事件也只能點(diǎn)擊原來的位置(即使它已經(jīng)運(yùn)動(dòng)到了別的位置),因?yàn)樗奈恢脧膩頉]有變過。我們不妨在動(dòng)畫完成之后重新設(shè)置它的位置。
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController () <CAAnimationDelegate>
@property (nonatomic, strong) CALayer *animationCALayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.animationCALayer = [[CALayer alloc] init];
self.animationCALayer.bounds = CGRectMake(0, 0, 40, 40);
self.animationCALayer.position = CGPointMake(50, 150);
self.animationCALayer.contents = (id)[UIImage imageNamed:@"vehicleResource.png"].CGImage;
[self.view.layer addSublayer:self.animationCALayer];
}
#pragma mark - 點(diǎn)擊事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = touches.anyObject;
CGPoint location = [touch locationInView:self.view];
[self animationWhitPoint:location];
}
#pragma mark - 移動(dòng)動(dòng)畫
- (void)animationWhitPoint:(CGPoint) location {
// 1、創(chuàng)建動(dòng)畫并指定動(dòng)畫屬性
CABasicAnimation *myBasicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
// 2、設(shè)置動(dòng)畫屬性
// myBasicAnimation.fromValue = [NSNumber numberWithInteger:50];
myBasicAnimation.toValue = [NSValue valueWithCGPoint:location];
// 設(shè)置動(dòng)畫其他屬性 事件 重復(fù)次數(shù) 運(yùn)行一次是否移除動(dòng)畫
myBasicAnimation.duration = 3;
// myBasicAnimation.repeatCount = 2;
// myBasicAnimation.removedOnCompletion = NO;
// 3、添加動(dòng)畫到圖層,注意key相當(dāng)于給動(dòng)畫進(jìn)行命名,以后獲得該動(dòng)畫時(shí)可以使用此名稱獲取
// 設(shè)置代理
myBasicAnimation.delegate = self;
// 存儲(chǔ)位置在動(dòng)畫結(jié)束后使用
[myBasicAnimation setValue:[NSValue valueWithCGPoint:location] forKey:@"KCBasicAnimationLocation"];
[self.animationCALayer addAnimation:myBasicAnimation forKey:@"KCBasicAnimation_Translation"];
}
#pragma mark - CAAnimationDelegate
/**
動(dòng)畫開始
*/
- (void)animationDidStart:(CAAnimation *)anim {
}
/**
動(dòng)畫結(jié)束
*/
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
self.animationCALayer.position = [[anim valueForKey:@"KCBasicAnimationLocation"] CGPointValue];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
?給動(dòng)畫設(shè)置代理監(jiān)聽開始和結(jié)束,存儲(chǔ)結(jié)束位置坐標(biāo),在動(dòng)畫結(jié)束時(shí)改變物體位置。但是問題是得到了解決,不過效果不佳,物體動(dòng)畫結(jié)束后又回到起始位置,然后瞬間移動(dòng)到結(jié)束位置。是因?yàn)閷?duì)于非根圖層,設(shè)置圖層的可動(dòng)畫屬性(在動(dòng)畫結(jié)束后重新設(shè)置了position,而position是可動(dòng)畫屬性)會(huì)產(chǎn)生動(dòng)畫效果。解決這個(gè)問題有兩種辦法:關(guān)閉圖層隱式動(dòng)畫、設(shè)置動(dòng)畫圖層為根圖層。我們這里用關(guān)閉圖層的隱式動(dòng)畫方法。只需要做如下修改:
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
// 開啟事務(wù)
[CATransaction begin];
// 禁用隱式動(dòng)畫
[CATransaction setDisableActions:YES];
self.animationCALayer.position = [[anim valueForKey:@"KCBasicAnimationLocation"] CGPointValue];
// 提交事務(wù)
[CATransaction commit];
}
還可以在移動(dòng)的過程中添加旋轉(zhuǎn)動(dòng)畫
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController () <CAAnimationDelegate>
@property (nonatomic, strong) CALayer *animationCALayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.animationCALayer = [[CALayer alloc] init];
self.animationCALayer.bounds = CGRectMake(0, 0, 40, 40);
self.animationCALayer.position = CGPointMake(50, 150);
self.animationCALayer.anchorPoint = CGPointMake(0.5, 0.5);
self.animationCALayer.contents = (id)[UIImage imageNamed:@"vehicleResource.png"].CGImage;
[self.view.layer addSublayer:self.animationCALayer];
}
#pragma mark - 點(diǎn)擊事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = touches.anyObject;
CGPoint location = [touch locationInView:self.view];
[self animationWhitPoint:location];
[self rotationAnimation];
}
#pragma mark - 移動(dòng)動(dòng)畫
- (void)animationWhitPoint:(CGPoint) location {
// 1、創(chuàng)建動(dòng)畫并指定動(dòng)畫屬性
CABasicAnimation *myBasicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
// 2、設(shè)置動(dòng)畫屬性
// myBasicAnimation.fromValue = [NSNumber numberWithInteger:50];
myBasicAnimation.toValue = [NSValue valueWithCGPoint:location];
// 設(shè)置動(dòng)畫其他屬性 事件 重復(fù)次數(shù) 運(yùn)行一次是否移除動(dòng)畫
myBasicAnimation.duration = 3;
// myBasicAnimation.repeatCount = 2;
// myBasicAnimation.removedOnCompletion = NO;
// 3、添加動(dòng)畫到圖層,注意key相當(dāng)于給動(dòng)畫進(jìn)行命名,以后獲得該動(dòng)畫時(shí)可以使用此名稱獲取
// 設(shè)置代理
myBasicAnimation.delegate = self;
// 存儲(chǔ)位置在動(dòng)畫結(jié)束后使用
[myBasicAnimation setValue:[NSValue valueWithCGPoint:location] forKey:@"KCBasicAnimationLocation"];
[self.animationCALayer addAnimation:myBasicAnimation forKey:@"KCBasicAnimation_Translation"];
}
#pragma mark - 旋轉(zhuǎn)動(dòng)畫
- (void) rotationAnimation {
// 1、創(chuàng)建動(dòng)畫并指定動(dòng)畫屬性
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
// 2、設(shè)置動(dòng)畫屬性初始值、結(jié)束值
// basicAnimation.fromValue = [NSNumber numberWithInt:M_PI_2];
basicAnimation.toValue = [NSNumber numberWithFloat:M_PI_2*3];
//設(shè)置其他動(dòng)畫屬性
basicAnimation.duration = 3.0;
basicAnimation.autoreverses = false; // 旋轉(zhuǎn)后再旋轉(zhuǎn)到原來的位置
// 3、添加動(dòng)畫到圖層,注意key相當(dāng)于給動(dòng)畫進(jìn)行命名,以后獲得該動(dòng)畫時(shí)可以使用此名稱獲取
[self.animationCALayer addAnimation:basicAnimation forKey:@"KCBasicAnimation_Rotation"];
}
#pragma mark - CAAnimationDelegate
/**
動(dòng)畫開始
*/
- (void)animationDidStart:(CAAnimation *)anim {
}
/**
動(dòng)畫結(jié)束
*/
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
// 開啟事務(wù)
[CATransaction begin];
// 禁用隱式動(dòng)畫
[CATransaction setDisableActions:YES];
self.animationCALayer.position = [[anim valueForKey:@"KCBasicAnimationLocation"] CGPointValue];
// 提交事務(wù)
[CATransaction commit];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
?彈性動(dòng)畫
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController () <CAAnimationDelegate>
@property (nonatomic, strong) CALayer *animationCALayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.animationCALayer = [[CALayer alloc] init];
self.animationCALayer.bounds = CGRectMake(0, 0, 40, 40);
self.animationCALayer.position = CGPointMake(50, 150);
self.animationCALayer.contents = (id)[UIImage imageNamed:@"image_3.jpg"].CGImage;
[self.view.layer addSublayer:self.animationCALayer];
}
#pragma mark - 點(diǎn)擊事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = touches.anyObject;
CGPoint location = [touch locationInView:self.view];
[self animationWhitPoint:location];
}
#pragma mark - 移動(dòng)動(dòng)畫
- (void)animationWhitPoint:(CGPoint) location {
// 1、創(chuàng)建動(dòng)畫并指定動(dòng)畫屬性
CASpringAnimation *mySpringAnimation = [CASpringAnimation animationWithKeyPath:@"position"];
mySpringAnimation.delegate = self;
// 2、設(shè)置動(dòng)畫屬性
mySpringAnimation.toValue = [NSValue valueWithCGPoint:location];
// damping:阻尼,范圍0-1,阻尼越接近于0,彈性效果越明顯
mySpringAnimation.damping = 0.6;
// initialVelocity:彈性復(fù)位的速度
mySpringAnimation.initialVelocity = 1.0;
// 設(shè)置動(dòng)畫其他屬性 事件 重復(fù)次數(shù) 運(yùn)行一次是否移除動(dòng)畫
mySpringAnimation.duration = 1;
[mySpringAnimation setValue:[NSValue valueWithCGPoint:location] forKey:@"KCBasicAnimationLocation"];
// 3、添加動(dòng)畫到圖層,注意key相當(dāng)于給動(dòng)畫進(jìn)行命名,以后獲得該動(dòng)畫時(shí)可以使用此名稱獲取
[self.animationCALayer addAnimation:mySpringAnimation forKey:@"KCBasicAnimation_Translation"];
}
#pragma mark - CAAnimationDelegate
/**
動(dòng)畫結(jié)束
*/
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
// 開啟事務(wù)
[CATransaction begin];
// 禁用隱式動(dòng)畫
[CATransaction setDisableActions:YES];
self.animationCALayer.position = [[anim valueForKey:@"KCBasicAnimationLocation"] CGPointValue];
// 提交事務(wù)
[CATransaction commit];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end