CAAnimation 核心動(dòng)畫

概念

  • Core Animation可以用在 Mac OS X 和 iOS平臺(tái). Core Animation的動(dòng)畫執(zhí)行過(guò)程是在后臺(tái)操作的.不會(huì)阻塞主線程. 要注意的是, Core Animation是直接作用在CALayer上的.并非UIView。

  • 使用步驟:
    1、創(chuàng)建一個(gè)CAAnimation對(duì)象
    2、設(shè)置一些動(dòng)畫的相關(guān)屬性
    3、給CALayer添加動(dòng)畫(addAnimation:forKey: 方法)
    4、停止CALayer動(dòng)畫(removeAnimationForKey: 方法)

  • 注意: 如果當(dāng)動(dòng)畫正在執(zhí)行的時(shí)候, 將程序退出到后臺(tái), 那么當(dāng)程序再次進(jìn)入前臺(tái)的時(shí)候就不執(zhí)行了。
    原因: 因?yàn)樵俅芜M(jìn)入前臺(tái)后動(dòng)畫已經(jīng)被刪除了。
    解決: anim.removedOnCompletion = NO;

CAAnimation繼承結(jié)構(gòu)

一、 CAAnimation

CAAnimation類是所有動(dòng)畫對(duì)象的父類,負(fù)責(zé)控制動(dòng)畫的持續(xù)時(shí)間和速度等,是個(gè)抽象類,不能直接使用,應(yīng)該使用它具體的子類

屬性:
  1. duration:動(dòng)畫的持續(xù)時(shí)間,默認(rèn)為0.25秒
  2. repeatCount:動(dòng)畫的重復(fù)次數(shù)
  3. repeatDuration:動(dòng)畫的重復(fù)時(shí)間
  4. removedOnCompletion:默認(rèn)為YES,代表動(dòng)畫執(zhí)行完畢后就從圖層上移除,圖形會(huì)恢復(fù)到動(dòng)畫執(zhí)行前的狀態(tài)。如果想讓圖層保持顯示動(dòng)畫執(zhí)行后的狀態(tài),那就設(shè)置為NO,不過(guò)還要設(shè)置fillMode屬性為kCAFillModeForwards
  5. fillMode:決定當(dāng)前對(duì)象在非active時(shí)間段的行為.比如動(dòng)畫開始之前,動(dòng)畫結(jié)束之后
  6. beginTime:可以用來(lái)設(shè)置動(dòng)畫延遲執(zhí)行時(shí)間,若想延遲2s,就設(shè)置為CACurrentMediaTime()+2,CACurrentMediaTime()為圖層的當(dāng)前時(shí)間
  7. timingFunction:速度控制函數(shù),控制動(dòng)畫運(yùn)行的節(jié)奏

枚舉參數(shù):
(1)kCAMediaTimingFunctionLinear 時(shí)間曲線函數(shù),勻速
(2)kCAMediaTimingFunctionEaseIn 時(shí)間曲線函數(shù),由慢到特別快
(3)kCAMediaTimingFunctionEaseOut 時(shí)間曲線函數(shù),由快到慢
(4)kCAMediaTimingFunctionEaseInEaseOut 時(shí)間曲線函數(shù),由慢到快
(5)kCAMediaTimingFunctionDefault 系統(tǒng)默認(rèn)

  1. delegate:動(dòng)畫代理,一般設(shè)置隱式代理,該代理是NSObject的分類,不需要遵守協(xié)議
    anim.delegate = self;
    (1)- (void)animationDidStart:(CAAnimation *)anim;核心動(dòng)畫開始時(shí)執(zhí)行
    (2)- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;核心動(dòng)畫執(zhí)行結(jié)束后調(diào)用

二、 CAPropertyAnimation

是CAAnimation的子類,也是個(gè)抽象類,要想創(chuàng)建動(dòng)畫對(duì)象,應(yīng)該使用它的兩個(gè)子類:CABasicAnimation和CAKeyframeAnimation

屬性:@property(nullable, copy) NSString *keyPath;
類方法:+ (instancetype)animationWithKeyPath:(nullableNSString *)path;

keyPath參數(shù):通過(guò)指定CALayer的一個(gè)屬性名做為keyPath里的參數(shù)(NSString類型),并且對(duì)CALayer的這個(gè)屬性的值進(jìn)行修改,達(dá)到相應(yīng)的動(dòng)畫效果。比如,指定@”position”為keyPath,就修改CALayer的position屬性的值,以達(dá)到平移的動(dòng)畫效果。
例子:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];

可修改的keyPath參數(shù):


keyPath參數(shù)

三、CABasicAnimation(基本動(dòng)畫)CAPropertyAnimation的子類

屬性:
  1. fromValue : keyPath相應(yīng)屬性的初始值
  2. toValue : keyPath相應(yīng)屬性的結(jié)束值,到某個(gè)固定的值(類似transform的make含義)
    注意:隨著動(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í)行前的初始值,并沒(méi)有真正被改變.比如: CALayer的postion初始值為(0,0),CABasicAnimation的fromValue為(10,10),toValue為 (100,100),雖然動(dòng)畫執(zhí)行完畢后圖層保持在(100,100) 這個(gè)位置,實(shí)質(zhì)上圖層的position還是為(0,0);
  3. byValue:不斷進(jìn)行累加的數(shù)值(類似transform非make方法的含義)
    例子:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.byValue = @(M_PI * 2);

四、 CAKeyframeAnimation(關(guān)鍵幀動(dòng)畫)CAPropertyAnimation的子類

和CABasicAnimation的區(qū)別:CABasicAnimation只能從一個(gè)數(shù)值(fromValue)變到另一個(gè)數(shù)值(toValue),而CAKeyframeAnimation會(huì)使用一個(gè)NSArray(values)保存這些數(shù)值,實(shí)現(xiàn)多個(gè)點(diǎn)間的動(dòng)畫效果,CABasicAnimation可看做是最多只有2個(gè)關(guān)鍵幀的CAKeyframeAnimation

屬性:
  1. values:NSArray對(duì)象,里面的元素稱為”關(guān)鍵幀”(NSValue類型),動(dòng)畫對(duì)象會(huì)在指定的時(shí)間(duration)內(nèi),依次顯示values數(shù)組中的每一個(gè)關(guān)鍵幀( NSValue)
    例子:
//設(shè)置動(dòng)畫屬性
NSValue *p1 = [NSValue valueWithCGPoint:CGPointMake(50, 150)];
NSValue *p2 = [NSValue valueWithCGPoint:CGPointMake(250, 150)];
NSValue *p3 = [NSValue valueWithCGPoint:CGPointMake(50, 550)];
NSValue *p4 = [NSValue valueWithCGPoint:CGPointMake(250, 550)];
animKey.values = @[p1, p2, p3, p4];
  1. path:可以設(shè)置一個(gè)CGPathRef\CGMutablePathRef,讓層跟著路徑移動(dòng),path只對(duì)CALayer的anchorPoint和position起作用,如果設(shè)置了path,那么values將被忽略。
    例子:
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 100, 250, 100)];
animKey.path = path.CGPath;
  1. keyTimes:可以為對(duì)應(yīng)的關(guān)鍵幀指定對(duì)應(yīng)的時(shí)間點(diǎn),其取值范圍為0到1.0,keyTimes中的每一個(gè)時(shí)間值都對(duì)應(yīng)values中的每一幀,當(dāng)keyTimes沒(méi)有設(shè)置的時(shí)候,各個(gè)關(guān)鍵幀的時(shí)間是平分的
  2. rotationMode:旋轉(zhuǎn)模式
    (1)如果為nil或不設(shè)置效果為
    旋轉(zhuǎn)模式效果1

    (2)設(shè)置為kCAAnimationRotateAuto 或 kCAAnimationRotateAutoReverse 會(huì)隨著旋轉(zhuǎn)的角度做 ”自轉(zhuǎn)“
    animKey.rotationMode = kCAAnimationRotateAuto; 效果為:
    旋轉(zhuǎn)模式效果2

五、 CAAnimationGroup(組動(dòng)畫)CAAnimation的子類

可以保存一組動(dòng)畫對(duì)象,將CAAnimationGroup對(duì)象加入層后,組中所有動(dòng)畫對(duì)象可以同時(shí)并發(fā)運(yùn)行

屬性:

animations:動(dòng)畫組,用來(lái)保存一組動(dòng)畫對(duì)象的NSArray
默認(rèn)情況下,一組動(dòng)畫對(duì)象是同時(shí)運(yùn)行的,也可以通過(guò)設(shè)置動(dòng)畫對(duì)象的beginTime屬性來(lái)更改動(dòng)畫的開始時(shí)間
例子:

// 2. 向組動(dòng)畫中添加各種子動(dòng)畫
// 2.1 旋轉(zhuǎn)
CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
// anim1.toValue = @(M_PI * 2 * 500);
anim1.byValue = @(M_PI * 2 * 1000);
// 2.2 縮放
CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
anim2.toValue = @(0.1);
// 2.3 改變位置, 修改position
CAKeyframeAnimation *anim3 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim3.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 100, 250, 100)].CGPath;

// 把子動(dòng)畫添加到組動(dòng)畫中
anim.animations = @[anim1, anim2, anim3];

六、CATransition(轉(zhuǎn)場(chǎng)動(dòng)畫)CAAnimation的子類

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

屬性:
  1. type:設(shè)置動(dòng)畫過(guò)渡的類型

枚舉:
kCATransitionFade 交叉淡化過(guò)渡
kCATransitionMoveIn 新視圖移到舊視圖上面
kCATransitionPush 新視圖把舊視圖推出去
kCATransitionReveal 將舊視圖移開,顯示下面的新視圖
下面類型包裝成字符串賦值

轉(zhuǎn)場(chǎng)動(dòng)畫過(guò)渡效果
  1. subtype:設(shè)置動(dòng)畫過(guò)渡方向

枚舉:
kCATransitionFromRight
kCATransitionFromLeft
kCATransitionFromTop
kCATransitionFromBottom

  1. startProgress:動(dòng)畫起點(diǎn)(在整體動(dòng)畫的百分比)
  2. endProgress:動(dòng)畫終點(diǎn)(在整體動(dòng)畫的百分比)
    例子:
- (IBAction)didRecognizeSwipeGesture:(UISwipeGestureRecognizer *)sender {
    // 1. 創(chuàng)建一個(gè)轉(zhuǎn)場(chǎng)動(dòng)畫對(duì)象
    CATransition *anim = [[CATransition alloc] init];
    // 設(shè)置轉(zhuǎn)場(chǎng)動(dòng)畫的類型
    anim.type = @"suckEffect";
    // 設(shè)置轉(zhuǎn)場(chǎng)動(dòng)畫時(shí)間
    anim.duration = 1.5;
    anim.delegate = self;

    // 判斷方向
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
        // 設(shè)置轉(zhuǎn)場(chǎng)動(dòng)畫的子類型
        anim.subtype = kCATransitionFromRight;
        // NSLog(@"left");
        self.index++;
    } else {
        // 設(shè)置轉(zhuǎn)場(chǎng)動(dòng)畫的子類型
        anim.subtype = kCATransitionFromLeft;
        // NSLog(@"right");
        self.index--;
    }
    // 判斷是否越界
    if (self.index > 4) {
        self.index = 0;
    }
    if (self.index < 0) {
        self.index = 4;
    }

    // 拼接圖片名稱
    NSString *imgName = [NSString stringWithFormat:@"%d", self.index + 1];
    // 切換圖片
    self.imgViewIcon.image = [UIImage imageNamed:imgName];
    // 把轉(zhuǎn)場(chǎng)動(dòng)畫添加到對(duì)應(yīng)的控件上
     [self.imgViewIcon.layer addAnimation:anim forKey:@"anim1"];
}

七、UIView的類方法實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫

單視圖:

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

雙視圖:

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

參數(shù)解析:
duration:動(dòng)畫的持續(xù)時(shí)間
view:需要進(jìn)行轉(zhuǎn)場(chǎng)動(dòng)畫的視圖
options:轉(zhuǎn)場(chǎng)動(dòng)畫的類型、效果,枚舉類型
animations:將改變視圖屬性的代碼放在這個(gè)block中
completion:動(dòng)畫結(jié)束后,會(huì)自動(dòng)調(diào)用這個(gè)block

例子:

// 識(shí)別到了輕掃手勢(shì)
- (IBAction)didRecognizeSwipeGesture:(UISwipeGestureRecognizer *)sender {
    // 判斷方向
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
        self.index++;
    } else {
        self.index--;
    }
    // 判斷是否越界
    if (self.index > 4) {
        self.index = 0;
    }
    if (self.index < 0) {
        self.index = 4;
    }
    // 拼接圖片名稱
    NSString *imgName = [NSString stringWithFormat:@"%d", self.index + 1];
    // 切換圖片
    self.imgViewIcon.image = [UIImage imageNamed:imgName];
    // 通過(guò)UIView來(lái)添加轉(zhuǎn)場(chǎng)動(dòng)畫
    [UIView transitionWithView:self.imgViewIcon duration:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
        [UIView animateWithDuration:0.5 animations:^{
            self.imgViewIcon.alpha = 0.4;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.5 animations:^{
                self.imgViewIcon.alpha = 1.0;
            }];
        }];
    } completion:^(BOOL finished) {
        NSLog(@"動(dòng)畫完成!");
    }];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 概念 Core Animation可以用在 Mac OS X 和 iOS平臺(tái). Core Animation的動(dòng)畫...
    就算我心狂野閱讀 828評(píng)論 0 0
  • 在iOS實(shí)際開發(fā)中常用的動(dòng)畫無(wú)非是以下四種:UIView動(dòng)畫,核心動(dòng)畫,幀動(dòng)畫,自定義轉(zhuǎn)場(chǎng)動(dòng)畫。 1.UIView...
    請(qǐng)叫我周小帥閱讀 3,169評(píng)論 1 23
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,572評(píng)論 6 30
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,141評(píng)論 5 13
  • 一只鳥的追憶 尊貴的人類: 你們好! 嘰嘰嘰,我是一只鳥兒。 中國(guó)的每...
    小紅帽0914閱讀 248評(píng)論 0 1