實(shí)現(xiàn)動(dòng)畫方式深度解析(十六) —— Core Animation之CABasicAnimation動(dòng)畫示例 (十三)

版本記錄

版本號(hào) 時(shí)間
V1.0 2017.09.24

前言

app中好的炫的動(dòng)畫可以讓用戶耳目一新,為產(chǎn)品增色不少,關(guān)于動(dòng)畫的實(shí)現(xiàn)我們可以用基本動(dòng)畫壤追、關(guān)鍵幀動(dòng)畫、序列幀動(dòng)畫以及基于CoreGraphic的動(dòng)畫等等供屉,接下來這幾篇我就介紹下我可以想到的幾種動(dòng)畫繪制方法行冰。具體Demo示例已開源到Github —— 刀客傳奇,感興趣的可以看我寫的另外幾篇伶丐。
1. 實(shí)現(xiàn)動(dòng)畫方式深度解析(一) —— 播放GIF動(dòng)畫(一)
2. 實(shí)現(xiàn)動(dòng)畫方式深度解析(二) —— 播放GIF動(dòng)畫之框架FLAnimatedImage的使用(二)
3. 實(shí)現(xiàn)動(dòng)畫方式深度解析(三) —— 播放序列幀動(dòng)畫(一)
4. 實(shí)現(xiàn)動(dòng)畫方式深度解析(四) —— QuartzCore框架(一)
5. 實(shí)現(xiàn)動(dòng)畫方式深度解析(五) —— QuartzCore框架之CoreAnimation(二)
6. 實(shí)現(xiàn)動(dòng)畫方式深度解析(六) —— Core Animation Basics(三)
7. 實(shí)現(xiàn)動(dòng)畫方式深度解析(七) —— Core Animation之Setting Up Layer Objects(四)
8. 實(shí)現(xiàn)動(dòng)畫方式深度解析(八) —— Core Animation之動(dòng)畫層內(nèi)容 (五)
9. 實(shí)現(xiàn)動(dòng)畫方式深度解析(九) —— Core Animation之構(gòu)建圖層層級(jí) (六)
10. 實(shí)現(xiàn)動(dòng)畫方式深度解析(十) —— Core Animation之高級(jí)動(dòng)畫技巧 (七)
11. 實(shí)現(xiàn)動(dòng)畫方式深度解析(十一) —— Core Animation之更改圖層的默認(rèn)行為(八)
12. 實(shí)現(xiàn)動(dòng)畫方式深度解析(十二) —— Core Animation之提高動(dòng)畫的性能(九)
13. 實(shí)現(xiàn)動(dòng)畫方式深度解析(十三) —— Core Animation之圖層樣式屬性動(dòng)畫(十)
14. 實(shí)現(xiàn)動(dòng)畫方式深度解析(十四) —— Core Animation之 KVC 擴(kuò)展(十一)
15. 實(shí)現(xiàn)動(dòng)畫方式深度解析(十五) —— Core Animation之可動(dòng)畫屬性 (十二)

CABasicAnimation基本

先看一下API文檔悼做。

/** Subclass for basic (single-keyframe) animations. **/

CA_CLASS_AVAILABLE (10.5, 2.0, 9.0, 2.0)
@interface CABasicAnimation : CAPropertyAnimation

/* The objects defining the property values being interpolated between.
 * All are optional, and no more than two should be non-nil. The object
 * type should match the type of the property being animated (using the
 * standard rules described in CALayer.h). The supported modes of
 * animation are:
 *
 * - both `fromValue' and `toValue' non-nil. Interpolates between
 * `fromValue' and `toValue'.
 *
 * - `fromValue' and `byValue' non-nil. Interpolates between
 * `fromValue' and `fromValue' plus `byValue'.
 *
 * - `byValue' and `toValue' non-nil. Interpolates between `toValue'
 * minus `byValue' and `toValue'.
 *
 * - `fromValue' non-nil. Interpolates between `fromValue' and the
 * current presentation value of the property.
 *
 * - `toValue' non-nil. Interpolates between the layer's current value
 * of the property in the render tree and `toValue'.
 *
 * - `byValue' non-nil. Interpolates between the layer's current value
 * of the property in the render tree and that plus `byValue'. */

@property(nullable, strong) id fromValue;
@property(nullable, strong) id toValue;
@property(nullable, strong) id byValue;

@end

它繼承了類CAPropertyAnimation

這里還要和大家啰嗦一下哗魂,Core Animation里面幾個(gè)類的層級(jí)關(guān)系肛走。

上面這個(gè)層級(jí)關(guān)系,大家一定要記住录别,有助于幫助大家理解這個(gè)結(jié)構(gòu)和功能使用朽色。


CABasicAnimation的幾個(gè)簡(jiǎn)單示例

下面我們就看一下CABasicAnimation的幾個(gè)簡(jiǎn)單示例。

1. 移動(dòng)

下面我們就直接看代碼组题。

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIView *animatedView;

@end

@implementation ViewController

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //設(shè)置UI
    [self setupUI];
    
    //位移動(dòng)畫
    [self demoPositionAnimation];
    
}

#pragma mark - Object Private Function

- (void)demoPositionAnimation
{
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    
    basicAnimation.duration = 2.0;
    basicAnimation.repeatCount = 100;
    
    //起始和終止位置
    basicAnimation.fromValue = [NSValue valueWithCGPoint:self.animatedView.layer.position];
    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300.0, 600.0)];
    
    //添加動(dòng)畫
    [self.animatedView.layer addAnimation:basicAnimation forKey:@"move-layer"];
}

- (void)setupUI
{
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.animatedView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)];
    self.animatedView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.animatedView];
}

@end

下面看一下效果驗(yàn)證

2. 旋轉(zhuǎn)

下面我們還是直接看代碼葫男。

- (void)demoRotationAnimation
{
    
    self.animatedView.frame = CGRectMake(50.0, 200.0, 200.0, 200.0);
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    
    basicAnimation.duration = 3.0;
    basicAnimation.repeatCount = 10;
    
    //起始和終止位置
    basicAnimation.fromValue = [NSNumber numberWithFloat:0.0];;
    basicAnimation.toValue = [NSNumber numberWithFloat:2 * M_PI];
    
    //添加動(dòng)畫
    [self.animatedView.layer addAnimation:basicAnimation forKey:@"rotate-layer"];
}

下面我們就看一下效果

3. 縮放

下面我們還是直接看代碼

- (void)demoScaleAnimation
{
    self.animatedView.frame = CGRectMake(50.0, 200.0, 200.0, 200.0);

    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    basicAnimation.duration = 3.0;
    basicAnimation.repeatCount = 10;
    basicAnimation.autoreverses = YES;
    
    //起始和終止位置
    basicAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    basicAnimation.toValue = [NSNumber numberWithFloat:0.4];
    
    //添加動(dòng)畫
    [self.animatedView.layer addAnimation:basicAnimation forKey:@"scale-layer"];
}

下面效果


與組動(dòng)畫CAAnimationGroup相關(guān)

我們可以利用組動(dòng)畫CAAnimationGroup進(jìn)行設(shè)置一組動(dòng)畫,下面我們就看一下代碼崔列。

- (void)demoGroupAnimation
{
    self.animatedView.frame = CGRectMake(50.0, 200.0, 100.0, 100.0);
    
    //向x方向移動(dòng)
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    moveAnimation.toValue = [NSNumber numberWithFloat:200.0];
    
    //繞z軸旋轉(zhuǎn)
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    rotationAnimation.toValue = [NSNumber numberWithFloat:2 * M_PI];
    
    //縮放
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.autoreverses = YES;
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:0.4];
    
    //動(dòng)畫組
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.duration = 3.0;
    animationGroup.repeatCount = 10;
    animationGroup.animations = [NSArray arrayWithObjects:moveAnimation, rotationAnimation, scaleAnimation, nil];
    
    [self.animatedView.layer addAnimation:animationGroup forKey:@"move-rotate-scale-layer"];
}

下面我們就看一下執(zhí)行效果


與代理相關(guān)

很多時(shí)候我們需要知道動(dòng)畫的開始和結(jié)束梢褐,以便可以在其中做邏輯處理。這個(gè)時(shí)候我們需要的就是代理赵讯。

下面我們就看一下代碼利职。

#import "ViewController.h"

@interface ViewController () <CAAnimationDelegate>

@property (nonatomic, strong) UIView *animatedView;

@end

@implementation ViewController

#pragma mark - override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //設(shè)置UI
    [self setupUI];
    
    //位移動(dòng)畫
    [self demoPositionAnimation];
}

#pragma mark - Object Private Function

- (void)demoPositionAnimation
{
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    
    basicAnimation.duration = 1.0;
    basicAnimation.repeatCount = 1;
    basicAnimation.delegate = self;
    
    //起始和終止位置
    basicAnimation.fromValue = [NSValue valueWithCGPoint:self.animatedView.layer.position];
    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300.0, 600.0)];
    
    //添加動(dòng)畫
    [self.animatedView.layer addAnimation:basicAnimation forKey:@"move-layer"];
}

- (void)setupUI
{
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.animatedView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)];
    self.animatedView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.animatedView];
}

#pragma mark - CAAnimationDelegate

- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"animation = %@", anim);
    NSLog(@"動(dòng)畫開始了");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"animation = %@", anim);
    NSLog(@"Flag = %d", flag);
    NSLog(@"動(dòng)畫結(jié)束了");
}

@end

下面看輸出結(jié)果

2017-09-24 13:49:49.472 JJCoreAnimation_demo7[3485:288887] animation = <CABasicAnimation: 0x60000003b600>
2017-09-24 13:49:49.472 JJCoreAnimation_demo7[3485:288887] 動(dòng)畫開始了
2017-09-24 13:49:50.470 JJCoreAnimation_demo7[3485:288887] animation = <CABasicAnimation: 0x60000003b600>
2017-09-24 13:49:50.470 JJCoreAnimation_demo7[3485:288887] Flag = 1
2017-09-24 13:49:50.470 JJCoreAnimation_demo7[3485:288887] 動(dòng)畫結(jié)束了

可見,實(shí)現(xiàn)了代理CAAnimationDelegate瘦癌,就可以監(jiān)測(cè)動(dòng)畫的開始和結(jié)束猪贪,可以做相應(yīng)的處理操作。

后記

未完讯私,待續(xù)~~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末热押,一起剝皮案震驚了整個(gè)濱河市西傀,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌桶癣,老刑警劉巖拥褂,帶你破解...
    沈念sama閱讀 218,036評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異牙寞,居然都是意外死亡饺鹃,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門间雀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悔详,“玉大人,你說我怎么就攤上這事惹挟∏洋Γ” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵连锯,是天一觀的道長(zhǎng)归苍。 經(jīng)常有香客問我,道長(zhǎng)运怖,這世上最難降的妖魔是什么拼弃? 我笑而不...
    開封第一講書人閱讀 58,622評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮摇展,結(jié)果婚禮上肴敛,老公的妹妹穿的比我還像新娘。我一直安慰自己吗购,他們只是感情好医男,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著捻勉,像睡著了一般镀梭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上踱启,一...
    開封第一講書人閱讀 51,521評(píng)論 1 304
  • 那天报账,我揣著相機(jī)與錄音,去河邊找鬼埠偿。 笑死透罢,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的冠蒋。 我是一名探鬼主播羽圃,決...
    沈念sama閱讀 40,288評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼抖剿!你這毒婦竟也來了朽寞?” 一聲冷哼從身側(cè)響起识窿,我...
    開封第一講書人閱讀 39,200評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎脑融,沒想到半個(gè)月后喻频,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡肘迎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評(píng)論 3 336
  • 正文 我和宋清朗相戀三年甥温,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片妓布。...
    茶點(diǎn)故事閱讀 39,953評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡姻蚓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出秋茫,到底是詐尸還是另有隱情,我是刑警寧澤乃秀,帶...
    沈念sama閱讀 35,673評(píng)論 5 346
  • 正文 年R本政府宣布肛著,位于F島的核電站,受9級(jí)特大地震影響跺讯,放射性物質(zhì)發(fā)生泄漏枢贿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評(píng)論 3 329
  • 文/蒙蒙 一刀脏、第九天 我趴在偏房一處隱蔽的房頂上張望局荚。 院中可真熱鬧,春花似錦愈污、人聲如沸耀态。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽首装。三九已至,卻和暖如春杭跪,著一層夾襖步出監(jiān)牢的瞬間仙逻,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工涧尿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留系奉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,119評(píng)論 3 370
  • 正文 我出身青樓姑廉,卻偏偏與公主長(zhǎng)得像缺亮,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子桥言,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評(píng)論 2 355

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