ios Core Animation(核心動(dòng)畫)

Core Animation,中文翻譯為核心動(dòng)畫弄唧,它是一組非常強(qiáng)大的動(dòng)畫處理API适肠,使用它能做出非常炫麗的動(dòng)畫效果,而且往往是事半功倍候引。也就是說侯养,使用少量的代碼就可以實(shí)現(xiàn)非常強(qiáng)大的功能。
Core Animation可以用在Mac OS X和iOS平臺(tái)澄干。
Core Animation的動(dòng)畫執(zhí)行過程都是在后臺(tái)操作的逛揩,不會(huì)阻塞主線程。
要注意的是麸俘,Core Animation是直接作用在CALayer上的辩稽,并非UIView。

一 从媚、Core Animation結(jié)構(gòu)

626233-43bafe84d8aee5bf.png

其中灰色虛線表示繼承關(guān)系逞泄,紅色表示遵守協(xié)議。

核心動(dòng)畫中所有類都遵守CAMediaTiming協(xié)議拜效。

1.CAAnaimation

是個(gè)抽象類喷众,不具備動(dòng)畫效果,必須用它的子類才有動(dòng)畫效果紧憾。是所有動(dòng)畫對(duì)象的父類到千,負(fù)責(zé)控制動(dòng)畫的持續(xù)時(shí)間和速度。

2.CAAnimationGroup和CATransition

是CAAnimation的子類,有動(dòng)畫效果稻励。
CAAnimationGroup是個(gè)動(dòng)畫組父阻,可以同時(shí)進(jìn)行縮放,旋轉(zhuǎn)(同時(shí)進(jìn)行多個(gè)動(dòng)畫)望抽。
CATransition是轉(zhuǎn)場(chǎng)動(dòng)畫加矛,界面之間跳轉(zhuǎn)(切換)都可以用轉(zhuǎn)場(chǎng)動(dòng)畫。

3.CAPropertyAnimation

是CAAnimation的子類煤篙,也是個(gè)抽象類斟览,本身不具備動(dòng)畫效果,只有子類才有辑奈。要想創(chuàng)建動(dòng)畫對(duì)象苛茂,應(yīng)該使用它的兩個(gè)子類:CABasicAnimation和CAKeyframeAnimation已烤。

4.CABasicAnimation和CAKeyframeAnimation

CABasicAnimation基本動(dòng)畫,做一些簡(jiǎn)單效果妓羊。
CAKeyframeAnimation幀動(dòng)畫胯究,做一些連續(xù)的流暢的動(dòng)畫。

二躁绸、基本使用

以基本動(dòng)畫為例:
先要有CALayer圖層裕循。
初始化一個(gè)CABasicAnimation對(duì)象,給對(duì)象設(shè)置相關(guān)的屬性净刮。
將基本動(dòng)畫對(duì)象添加到CALayer對(duì)象中就可以開始動(dòng)畫了剥哑。

CALayer *layer = [CALayer layer];
...
CABasicAnimation *animation = [CABasicAnimation animation];
anmation.keyPath = @"transform.scale";
anmation.toValue = @0;
[layer addAnimation:animation forKey:nil];

三、CAAnimation

1淹父、CABasicAnimation(基礎(chǔ)動(dòng)畫)

CAPropertyAnimation的子類
屬性解析:
fromValue:keyPath相應(yīng)屬性的初始值
toValue:keyPath相應(yīng)屬性的結(jié)束值
隨著動(dòng)畫的進(jìn)行株婴,在長(zhǎng)度為duration的持續(xù)時(shí)間內(nèi),keyPath相應(yīng)屬性的值從fromValue漸漸地變?yōu)閠oValue
如果fillMode=kCAFillModeForwards和removedOnComletion=NO暑认,那么在動(dòng)畫執(zhí)行完畢后困介,圖層會(huì)保持顯示動(dòng)畫執(zhí)行后的狀態(tài)。但在實(shí)質(zhì)上穷吮,圖層的屬性值還是動(dòng)畫執(zhí)行前的初始值逻翁,并沒有真正被改變。
比如捡鱼,CALayer的position初始值為(0,0),CABasicAnimation的fromValue為(10,10)酷愧,toValue為(100,100)驾诈,雖然動(dòng)畫執(zhí)行完畢后圖層保持在(100,100)這個(gè)位置,實(shí)質(zhì)上圖層的position還是為(0,0)

平移動(dòng)畫
#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è)置動(dòng)畫(基礎(chǔ)動(dòng)畫)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.創(chuàng)建核心動(dòng)畫
    //    CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>]
    CABasicAnimation *anima=[CABasicAnimation animation];
    
    //1.1告訴系統(tǒng)要執(zhí)行什么樣的動(dòng)畫
    anima.keyPath=@"position";
    //設(shè)置通過動(dòng)畫溶浴,將layer從哪兒移動(dòng)到哪兒
    anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
    anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
    
    //1.2設(shè)置動(dòng)畫執(zhí)行完畢之后不刪除動(dòng)畫
    anima.removedOnCompletion=NO;
    //1.3設(shè)置保存動(dòng)畫的最新狀態(tài)
    anima.fillMode=kCAFillModeForwards;
    anima.delegate=self;
    //打印
    NSString *str=NSStringFromCGPoint(self.myLayer.position);
    NSLog(@"執(zhí)行前:%@",str);
    
    //2.添加核心動(dòng)畫到layer
    [self.myLayer addAnimation:anima forKey:nil];

}

-(void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"開始執(zhí)行動(dòng)畫");
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    //動(dòng)畫執(zhí)行完畢乍迄,打印執(zhí)行完畢后的position值
    NSString *str=NSStringFromCGPoint(self.myLayer.position);
    NSLog(@"執(zhí)行后:%@",str);
}

@end
縮放動(dòng)畫
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.創(chuàng)建動(dòng)畫
    CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"bounds"];
    //1.1設(shè)置動(dòng)畫執(zhí)行時(shí)間
    anima.duration=2.0;
    //1.2設(shè)置動(dòng)畫執(zhí)行完畢后不刪除動(dòng)畫
    anima.removedOnCompletion=NO;
    //1.3設(shè)置保存動(dòng)畫的最新狀態(tài)
    anima.fillMode=kCAFillModeForwards;
    //1.4修改屬性,執(zhí)行動(dòng)畫
    anima.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
    //2.添加動(dòng)畫到layer
    [self.myLayer addAnimation:anima forKey:nil];
}
旋轉(zhuǎn)動(dòng)畫
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.創(chuàng)建動(dòng)畫
    CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"transform"];
    //1.1設(shè)置動(dòng)畫執(zhí)行時(shí)間
    anima.duration=2.0;
    //1.2修改屬性士败,執(zhí)行動(dòng)畫
    anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
    //1.3設(shè)置動(dòng)畫執(zhí)行完畢后不刪除動(dòng)畫
    anima.removedOnCompletion=NO;
    //1.4設(shè)置保存動(dòng)畫的最新狀態(tài)
    anima.fillMode=kCAFillModeForwards;
    
    //2.添加動(dòng)畫到layer
    [self.myLayer addAnimation:anima forKey:nil];
}

2闯两、CAKeyframeAnimation(關(guān)鍵幀動(dòng)畫)

關(guān)鍵幀動(dòng)畫,也是CAPropertyAnimation的子類谅将,與CABasicAnimation的區(qū)別是:
CABasicAnimation只能從一個(gè)數(shù)值(fromValue)變到另一個(gè)數(shù)值(toValue)漾狼,而CAKeyframeAnimation會(huì)使用一個(gè)NSArray保存這些數(shù)值
CABasicAnimation可看做是只有2個(gè)關(guān)鍵幀的CAKeyframeAnimation

修改位置
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 1.創(chuàng)建關(guān)鍵幀動(dòng)畫
    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)抖動(dòng)效果
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 1.創(chuàng)建關(guān)鍵幀動(dòng)畫
    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(動(dòng)畫組)

動(dòng)畫組,是CAAnimation的子類饥臂,可以保存一組動(dòng)畫對(duì)象逊躁,將CAAnimationGroup對(duì)象加入層后,組中所有動(dòng)畫對(duì)象可以同時(shí)并發(fā)運(yùn)行隅熙。
默認(rèn)情況下,一組動(dòng)畫對(duì)象是同時(shí)運(yùn)行的稽煤,也可以通過設(shè)置動(dòng)畫對(duì)象的beginTime屬性來更改動(dòng)畫的開始時(shí)間核芽。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // 1.創(chuàng)建組動(dòng)畫對(duì)象
    CAAnimationGroup *group = [CAAnimationGroup animation];

    // 2.設(shè)置動(dòng)畫屬性
    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)場(chǎng)動(dòng)畫)

CATransition是CAAnimation的子類酵熙,用于做轉(zhuǎn)場(chǎng)動(dòng)畫轧简,能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭?dòng)畫效果。iOS比Mac OS X的轉(zhuǎn)場(chǎng)動(dòng)畫效果少一點(diǎn)匾二。
UINavigationController就是通過CATransition實(shí)現(xiàn)了將控制器的視圖推入屏幕的動(dòng)畫效果吉懊。

#pragma mark - 2.輕掃觸發(fā)
- (void)swipeAction:(UISwipeGestureRecognizer *)recognizer {

    // MARK: - 轉(zhuǎn)場(chǎng)動(dòng)畫
    // 1.創(chuàng)建動(dòng)畫對(duì)象
    CATransition *anim = [CATransition animation];
    
    // 兩個(gè)屬性 type[動(dòng)畫的類型], subType[子類型]
    
    // 2.設(shè)置動(dòng)畫屬性
    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.添加動(dòng)畫
    [imgView.layer addAnimation:anim forKey:nil];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市假勿,隨后出現(xiàn)的幾起案子借嗽,更是在濱河造成了極大的恐慌,老刑警劉巖转培,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件恶导,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡浸须,警方通過查閱死者的電腦和手機(jī)惨寿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來删窒,“玉大人裂垦,你說我怎么就攤上這事〖∷鳎” “怎么了蕉拢?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)诚亚。 經(jīng)常有香客問我晕换,道長(zhǎng),這世上最難降的妖魔是什么站宗? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任闸准,我火速辦了婚禮,結(jié)果婚禮上梢灭,老公的妹妹穿的比我還像新娘夷家。我一直安慰自己,他們只是感情好敏释,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布库快。 她就那樣靜靜地躺著,像睡著了一般颂暇。 火紅的嫁衣襯著肌膚如雪缺谴。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音湿蛔,去河邊找鬼膀曾。 笑死,一個(gè)胖子當(dāng)著我的面吹牛阳啥,可吹牛的內(nèi)容都是我干的添谊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼察迟,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼斩狱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起扎瓶,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤所踊,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后概荷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秕岛,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年误证,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了继薛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡愈捅,死狀恐怖遏考,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蓝谨,我是刑警寧澤灌具,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站像棘,受9級(jí)特大地震影響稽亏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜缕题,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望胖腾。 院中可真熱鬧烟零,春花似錦、人聲如沸咸作。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽记罚。三九已至墅诡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間桐智,已是汗流浹背末早。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國打工烟馅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人然磷。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓郑趁,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親姿搜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子寡润,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

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