iOS Core Animation (一) 基礎(chǔ)動(dòng)畫

iOS核心動(dòng)畫Core Animation分為幾類:基礎(chǔ)動(dòng)畫、關(guān)鍵幀動(dòng)畫、動(dòng)畫組、轉(zhuǎn)場(chǎng)動(dòng)畫。關(guān)系大致如下圖:


關(guān)系圖.png

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
動(dòng)態(tài)圖1.gif

基本效果已經(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)態(tài)圖2.gif

?給動(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)態(tài)圖3.gif

?彈性動(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
動(dòng)態(tài)圖4.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,533評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,055評(píng)論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,365評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,561評(píng)論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,346評(píng)論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,889評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,978評(píng)論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,118評(píng)論 0 286
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,637評(píng)論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,558評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,739評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,246評(píng)論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 43,980評(píng)論 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,619評(píng)論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,347評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,702評(píng)論 2 370

推薦閱讀更多精彩內(nèi)容