iOS 核心動畫

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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末才漆,一起剝皮案震驚了整個濱河市牛曹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌醇滥,老刑警劉巖黎比,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異腺办,居然都是意外死亡焰手,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門怀喉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人船响,你說我怎么就攤上這事躬拢《懵模” “怎么了?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵聊闯,是天一觀的道長工猜。 經(jīng)常有香客問我,道長菱蔬,這世上最難降的妖魔是什么篷帅? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮拴泌,結(jié)果婚禮上魏身,老公的妹妹穿的比我還像新娘。我一直安慰自己蚪腐,他們只是感情好箭昵,可當(dāng)我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著回季,像睡著了一般家制。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上泡一,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天颤殴,我揣著相機與錄音,去河邊找鬼鼻忠。 笑死涵但,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的粥烁。 我是一名探鬼主播贤笆,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼讨阻!你這毒婦竟也來了芥永?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤钝吮,失蹤者是張志新(化名)和其女友劉穎埋涧,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體奇瘦,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡棘催,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了耳标。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片醇坝。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖次坡,靈堂內(nèi)的尸體忽然破棺而出呼猪,到底是詐尸還是另有隱情画畅,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布宋距,位于F島的核電站轴踱,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏谚赎。R本人自食惡果不足惜淫僻,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望壶唤。 院中可真熱鬧雳灵,春花似錦、人聲如沸视粮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蕾殴。三九已至笑撞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間钓觉,已是汗流浹背茴肥。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留荡灾,地道東北人瓤狐。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像批幌,于是被迫代替她去往敵國和親础锐。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,601評論 2 353

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