iOS - Core Animation 核心動畫

1、UIView 動畫

  • 具體講解見 iOS - UIView 動畫

2、UIImageView 動畫

  • 具體講解見 iOS - UIImageView 動畫

3、CADisplayLink 定時器

  • 具體講解見 iOS - OC NSTimer 定時器

  • CADisplayLink 是一個能讓我們以和屏幕刷新率相同的頻率將內容畫到屏幕上的定時器。我們在應用中創建一個新的 CADisplayLink 對象,把它添加到一個 runloop 中,并給它提供一個 target 和 selector 在屏幕刷新的時候調用。

  • CADisplayLink 使用場合相對專一,適合做 UI 的不停重繪,比如自定義動畫引擎或者視頻播放的渲染。NSTimer 的使用范圍要廣泛的多,各種需要單次或者循環定時處理的任務都可以使用。在 UI 相關的動畫或者顯示內容使用 CADisplayLink 比起用 NSTimer 的好處就是我們不需要再格外關心屏幕的刷新頻率了,因為它本身就是跟屏幕刷新同步的。

4、CALayer 繪圖層

  • 具體講解見 iOS - CALayer 繪圖層

  • 在 iOS 系統中,你能看得見摸得著的東西基本上都是 UIView,比如一個按鈕、一個文本標簽、一個文本輸入框、一個圖標等等,這些都是 UIView。其實 UIView 之所以能顯示在屏幕上,完全是因為它內部的一個層。在創建 UIView 對象時,UIView 內部會自動創建一個層(即 CALayer 對象),通過 UIView 的 layer 屬性可以訪問這個層。當 UIView 需要顯示到屏幕上時,會調用 drawRect: 方法進行繪圖,并且會將所有內容繪制在自己的層上,繪圖完畢后,系統會將層拷貝到屏幕上,于是就完成了 UIView 的顯示。換句話說,UIView 本身不具備顯示的功能,是它內部的層才有顯示功能。

5、核心動畫基本概念

  • Core Animation 中文翻譯為核心動畫,它是一組非常強大的動畫處理 API,使用它能做出非常炫麗的動畫效果,而且往往是事半功倍。也就是說,使用少量的代碼就可以實現非常強大的功能。Core Animation 可以用在 macOS 和 iOS 平臺。喬幫主在 2007 年的 WWDC 大會上親自為你演示 Core Animation 的強大:點擊查看視頻。

  • Core Animation 的動畫執行過程都是在后臺操作的,不會阻塞主線程。

  • 要注意的是,Core Animation 是直接作用在 CALayer 上的,并非 UIView。

  • 核心動畫 和 UIView 動畫 的區別:

    • 核心動畫一切都是假象,并不會真實的改變圖層的屬性值,如果以后做動畫的時候,不需要與用戶交互,通常用核心動畫(轉場)。
    • UIView 動畫必須通過修改屬性的真實值,才有動畫效果。

5.1 核心動畫繼承結構

  • 核心動畫繼承結構

    animation30

5.2 核心動畫使用步驟

  • Xcode6 之前的版本,使用它需要先添加 QuartzCore.framework 和引入對應的框架 <QuartzCore/QuartzCore.h>

  • 開發步驟:

    • 1、首先得有 CALayer。
    • 2、初始化一個 CAAnimation 對象,并設置一些動畫相關屬性。
    • 3、通過調用 CALayer 的 addAnimation:forKey: 方法,增加 CAAnimation 對象到 CALayer 中,這樣就能開始執行動畫了。
    • 4、通過調用 CALayer 的 removeAnimationForKey: 方法可以停止 CALayer 中的動畫。

5.3 CAAnimation

  • 1、CAAnimation 簡介

    • CAAnimation 是所有動畫對象的父類,負責控制動畫的持續時間和速度,是個抽象類,不能直接使用,應該使用它具體的子類。

    • 屬性說明:

          removedOnCompletion :默認為 YES,代表動畫執行完畢后就從圖層上移除,圖形會恢復到動畫執行前的狀態。如果想讓圖層保持顯示動畫執行后的狀態,那就設置為 NO,不過還要設置 fillMode 為 kCAFillModeForwards。timingFunction      :速度控制函數,控制動畫運行的節奏。delegate            :動畫代理,需要遵守協議 CAAnimationDelegate。// 來自 CAMediaTiming 協議的屬性duration       :動畫的持續時間。repeatCount    :重復次數,無限循環可以設置 HUGE_VALF 或者 MAXFLOAT。repeatDuration :重復時間。fillMode       :決定當前對象在非 active 時間段的行為。比如動畫開始之前或者動畫結束之后。beginTime      :可以用來設置動畫延遲執行時間,若想延遲 2s,就設置為 CACurrentMediaTime()+2,CACurrentMediaTime() 為圖層的當前時間。
  • 2、CAAnimation 動畫填充模式

    • 設置 fillMode 屬性值(要想 fillMode 有效,需要設置 removedOnCompletion = NO)

          kCAFillModeRemoved   :這個是默認值,也就是說當動畫開始前和動畫結束后,動畫對 layer 都沒有影響,動畫結束后,layer 會恢復到之前的狀態。kCAFillModeForwards  :當動畫結束后,layer 會一直保持著動畫最后的狀態。kCAFillModeBackwards :在動畫開始前,只需要將動畫加入了一個 layer,layer 便立即進入動畫的初始狀態并等待動畫開始。kCAFillModeBoth      :這個其實就是上面兩個的合成,動畫加入后開始之前,layer 便處于動畫初始狀態,動畫結束后 layer 保持動畫最后的狀態。
  • 3、CAAnimation 速度控制函數

    • CAMediaTimingFunction 速度控制函數

          kCAMediaTimingFunctionLinear        :線性,勻速,給你一個相對靜態的感覺。kCAMediaTimingFunctionEaseIn        :漸進,動畫緩慢進入,然后加速離開。kCAMediaTimingFunctionEaseOut       :漸出,動畫全速進入,然后減速的到達目的地。kCAMediaTimingFunctionEaseInEaseOut :漸進漸出,動畫緩慢的進入,中間加速,然后減速的到達目的地。這個是默認的動畫行為。
  • 4、CAAnimation 動畫代理方法

    • CAAnimation 在分類中定義了代理方法

          @interface NSObject (CAAnimationDelegate)/* Called when the animation begins its active duration. */// 動畫開始執行的時候觸發這個方法- (void)animationDidStart:(CAAnimation *)anim;/* Called when the animation either completes its active duration or* is removed from the object it is attached to (i.e. the layer). 'flag'* is true if the animation reached the end of its active duration* without being removed. */// 動畫執行完畢的時候觸發這個方法- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;@end

5.4 CAPropertyAnimation

  • CAPropertyAnimation 是 CAAnimation 的子類,也是個抽象類,要想創建動畫對象,應該使用它的兩個子類:
    • CABasicAnimation
    • CAKeyframeAnimation
  • 屬性說明:

        keyPath :通過指定 CALayer 的一個屬性名稱為 keyPath(NSString 類型),并且對 CALayer 的這個屬性的值進行修改,達到相應的動畫效果。比如,指定 @“position” 為 keyPath,就修改 CALayer 的 position 屬性的值,以達到平移的動畫效果。

5.5 CALayer 上動畫的暫停和恢復

  • 1、CALayer 上動畫的暫停

        // 暫停 CALayer 的動畫,自定義方法- (void)pauseLayer:(CALayer*)layer {CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];// 讓 CALayer 的時間停止走動layer.speed = 0.0;// 讓 CALayer 的時間停留在 pausedTime 這個時刻layer.timeOffset = pausedTime;}
  • 2、CALayer 上動畫的恢復

        // 恢復 CALayer 的動畫,自定義方法- (void)resumeLayer:(CALayer*)layer {CFTimeInterval pausedTime = layer.timeOffset;// 讓 CALayer 的時間繼續行走layer.speed = 1.0;// 取消上次記錄的停留時刻layer.timeOffset = 0.0;// 取消上次設置的時間layer.beginTime = 0.0;// 計算暫停的時間(這里也可以用 CACurrentMediaTime() - pausedTime)CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;// 設置相對于父坐標系的開始時間(往后退 timeSincePause)layer.beginTime = timeSincePause;}

6、基本動畫

  • CABasicAnimation 基本動畫,是 CAPropertyAnimation 的子類。

  • 屬性說明:

        fromValue :keyPath 相應屬性的初始值toValue   :keyPath 相應屬性的結束值byValue   :keyPath 相應屬性的改變值
  • 動畫過程說明:

    • keyPath 內容是 CALayer 的可動畫 Animatable 屬性。
    • 隨著動畫的進行,在長度為 duration 的持續時間內,keyPath 相應屬性的值從 fromValue 漸漸地變為 toValue。
    • 如果 fillMode = kCAFillModeForwards 同時 removedOnComletion = NO,那么在動畫執行完畢后,圖層會保持顯示動畫執行后的狀態。但在實質上,圖層的屬性值還是動畫執行前的初始值,并沒有真正被改變。
  • CABasicAnimation 雖然能夠做很多基本的動畫效果,但是有個局限性,只能讓 CALayer 的屬性從某個值漸變到另一個值,僅僅是在 2 個值之間漸變。

6.1 平移動畫

  • 1、方法 1

        // 說明這個動畫對象要對 CALayer 的 position 屬性執行動畫CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];// 動畫持續 1.5sanim.duration = 1.5;// position 屬性值從 (50, 80) 漸變到 (300, 350)anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 80)];anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 350)];// 設置動畫的代理anim.delegate = self;// 保持動畫執行后的狀態anim.removedOnCompletion = NO;anim.fillMode = kCAFillModeForwards;// 添加動畫對象到圖層上[self.myView.layer addAnimation:anim forKey:@"translate"];
    • 第 2 行設置的 keyPath 是 @"position",說明要修改的是 CALayer 的 position 屬性,也就是會執行平移動畫。

    • 注意第 8、9 行,這里并不是直接使用 CGPoint 這種結構體類型,而是要先包裝成 NSValue 對象后再使用。這 2 行代碼表示 CALayer 從位置 (50, 80) 移動到位置 (250, 350)。

    • 如果將第 9 行的 toValue 換成 byValue,代表 CALayer 從位置 (50, 80) 開始向右移動 250、向下移動 350,也就是移動到位置 (300, 430)。

          anim.byValue = [NSValue valueWithCGPoint:CGPointMake(250, 350)];
    • 默認情況下,動畫執行完畢后,動畫會自動從 CALayer 上移除,CALayer 又會回到原來的狀態。為了保持動畫執行后的狀態,可以加入第 15、16 行代碼。

    • 第 19 行后面的 @"translate" 是給動畫對象起個名稱,以后可以調用 CALayer 的 removeAnimationForKey: 方法根據動畫名稱停止相應的動畫。

    • 第 12 行是設置動畫的代理,可以監聽動畫的執行過程,這里設置控制器為代理。需要遵守協議 CAAnimationDelegate,代理需要實現的方法有:

          - (void)animationDidStart:(CAAnimation *)anim {NSLog(@"%s", __func__);}- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {NSLog(@"%s", __func__);}
    • 打印結果為:

          09:11:10.880 CALayerDemo[12721:622836] -[ViewController animationDidStart:]09:11:12.419 CALayerDemo[12721:622836] -[ViewController animationDidStop:finished:],position:{50, 80}
    • 實際上,動畫執行完畢后,并沒有真正改變 CALayer 的 position 屬性的值。

    • 效果

      animation32

  • 2、方法 2

        // 說明這個動畫對象要對 CALayer 的 transform 屬性執行動畫CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];// 動畫持續 1.5sanim.duration = 1.5;// 平移,x 軸移動 250,y 軸移動 350,z 軸移動 0CATransform3D form = CATransform3DMakeTranslation(250, 350, 0);anim.toValue = [NSValue valueWithCATransform3D:form];// 添加動畫對象到圖層上[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation33

6.2 縮放動畫

  • 1、方法 1

        // 說明這個動畫對象要對 CALayer 的 bounds 屬性執行動畫CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];// 動畫持續 2sanim.duration = 2;// 縮放,width 從 100 變為 30,height 從 100 變為 30// anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];  // fromValue 可以省略anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 30, 30)];// 添加動畫對象到圖層上[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation34

  • 2、方法 2

        // 說明這個動畫對象要對 CALayer 的 transform 屬性執行動畫CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];// 動畫持續 1.5sanim.duration = 1.5;// 縮放,width 從 100 變為 200,height 從 100 變為 150// anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];anim.toValue  = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 1.5, 1.0)];// 添加動畫對象到圖層上[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation35

  • 3、心跳效果

        // 說明這個動畫對象要對 CALayer 的 transform.scale 屬性執行動畫CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];// 動畫持續 1.0sanim.duration = 1.0;// 縮放倍數anim.toValue = @0.5;// 設置動畫執行次數anim.repeatCount = MAXFLOAT;// 保持動畫執行后的狀態anim.removedOnCompletion = NO;anim.fillMode = kCAFillModeForwards;// 添加動畫對象到圖層上[imageView.layer addAnimation:anim forKey:nil];
    • 效果

      animation42

6.3 旋轉動畫

  • 1、方法

        // 說明這個動畫對象要對 CALayer 的 transform.rotation 屬性執行動畫CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];// 動畫持續 3.5sanim.duration = 3.5;// 縮放倍數anim.toValue = @(M_PI_4 * 3);// 添加動畫對象到圖層上[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation43

  • 2、方法

        // 說明這個動畫對象要對 CALayer 的 transform 屬性執行動畫CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];// 動畫持續 3.5sanim.duration = 3.5;// 繞著 (0, 0, 1) 這個向量軸順時針旋轉 45°// anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0, 0, 1)];anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4 * 3, 0, 0, 1)];// 設置動畫執行次數anim.repeatCount = MAXFLOAT;// 保持動畫執行后的狀態anim.removedOnCompletion = NO;anim.fillMode = kCAFillModeForwards;// 添加動畫對象到圖層上[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation36

7、關鍵幀動畫

  • CAKeyframeAnimation 關鍵幀動畫,是 CAPropertyAnimation 的子類。

  • 與 CABasicAnimation 的區別是:

    • CABasicAnimation 只能從一個數值(fromValue)變到另一個數值(toValue),而 CAKeyframeAnimation 會使用一個 NSArray 保存這些數值。
    • CABasicAnimation 可看做是只有2個關鍵幀的 CAKeyframeAnimation。
  • 屬性說明:

        values   :上述的 NSArray 對象。里面的元素稱為 “關鍵幀” (keyframe)。動畫對象會在指定的時間(duration)內,依次顯示 values 數組中的每一個關鍵幀。path     :可以設置一個 CGPathRef、CGMutablePathRef,讓圖層按照路徑軌跡移動。path 只對 CALayer 的 anchorPoint 和 position 起作用。如果設置了 path,那么 values 將被忽略。keyTimes :可以為對應的關鍵幀指定對應的時間點,其取值范圍為 0 到 1.0,keyTimes 中的每一個時間值都對應 values 中的每一幀。如果沒有設置 keyTimes,各個關鍵幀的時間是平分的。

7.1 平移動畫

  • 1、添加動畫關鍵幀

        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation"];// CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];anim.duration = 5.0;// 動畫起始位置,從 position 點出開始NSValue *pointVal1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];// 水平移動 200 - 0,垂直方向不變(0 - 0 = 0)NSValue *pointVal2 = [NSValue valueWithCGPoint:CGPointMake(200, 0)];// 垂直移動 200 - 0,水平位置不變(200 - 200 = 0)NSValue *pointVal3 = [NSValue valueWithCGPoint:CGPointMake(200, 200)];// 水平移動 0 - 200,垂直方向不變(200 - 200 = 0)NSValue *pointVal4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)];// 垂直移動 0 - 200,水平位置不變(0 - 0 = 0)NSValue *pointVal5 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];// 添加動畫關鍵幀anim.values = @[pointVal1, pointVal2, pointVal3, pointVal4, pointVal5];[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation44

  • 2、添加動畫路徑

        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation"];// CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];anim.duration = 5.0;// 添加動畫路徑anim.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 200, 200)].CGPath;[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation44

  • 3、劃定路徑移動

    • DrawView.m

          @interface DrawView ()@property (nonatomic, strong) UIBezierPath *path;@end@implementation DrawView/// 觸摸開始- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {// 移除上一個動畫[self.subviews.firstObject.layer removeAnimationForKey:@"drawAnimation"];// 獲取觸摸起始點位置CGPoint startPoint = [touches.anyObject locationInView:self];// 創建路徑self.path = [UIBezierPath bezierPath];// 設置起點[self.path moveToPoint:startPoint];}/// 觸摸移動- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {// 獲取觸摸點位置CGPoint touchPoint = [touches.anyObject locationInView:self];[self.path addLineToPoint:touchPoint];[self setNeedsDisplay];}/// 觸摸結束- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {// 添加關鍵幀動畫CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];anim.keyPath = @"position";// 添加動畫路徑anim.path = self.path.CGPath;anim.duration = 3;anim.repeatCount = MAXFLOAT;anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];// 給子視圖添加核心動畫[self.subviews.firstObject.layer addAnimation:anim forKey:@"drawAnimation"];}/// 觸摸取消- (void)touchesCancelled:(NSSet *)touches withEvent:(nullable UIEvent *)event {[self touchesEnded:touches withEvent:event];}/// 繪圖- (void)drawRect:(CGRect)rect {[self.path stroke];}@end
    • ViewController.m

          DrawView *myDrawView = [[DrawView alloc] initWithFrame:self.view.bounds];myDrawView.backgroundColor = [UIColor whiteColor];[self.view addSubview:myDrawView];UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];imv.image = [UIImage imageNamed:@"heart2"];[myDrawView addSubview:imv];
      • 效果

        animation47

7.2 縮放動畫

  • 縮放動畫

        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];anim.duration = 5.0;// 動畫起始大小,1 不縮放NSNumber *scaleVal1 = @1;// 放大 2 倍NSNumber *scaleVal2 = @2;// 不縮放,縮小至原來大小NSNumber *scaleVal3 = @1;// 縮小 0.5 倍NSNumber *scaleVal4 = @0.5;// 不縮放,放大至原來大小NSNumber *scaleVal5 = @1;// 添加動畫關鍵幀anim.values = @[scaleVal1, scaleVal2, scaleVal3, scaleVal4, scaleVal5];[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation45

7.3 旋轉動畫

  • 1、旋轉動畫

        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];anim.duration = 5.0;// 動畫起始角度,0 不旋轉NSNumber *rotationVal1 = @0;// 旋轉 M_PI_4 * 1NSNumber *rotationVal2 = @(M_PI_4 * 1);// 旋轉 M_PI_4 * 2NSNumber *rotationVal3 = @(M_PI_4 * 2);// 旋轉 M_PI_4 * 3NSNumber *rotationVal4 = @(M_PI_4 * 3);// 旋轉 M_PI_4 * 4NSNumber *rotationVal5 = @(M_PI_4 * 4);// 添加動畫關鍵幀anim.values = @[rotationVal1, rotationVal2, rotationVal3, rotationVal4, rotationVal5];[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation46

  • 2、圖標抖動

        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];anim.repeatCount = MAXFLOAT;NSNumber *rotationVal1 = @(-5 / 180.0 * M_PI);NSNumber *rotationVal2 = @(5 / 180.0 * M_PI);NSNumber *rotationVal3 = @(-5 / 180.0 * M_PI);// 添加動畫關鍵幀anim.values = @[rotationVal1, rotationVal2, rotationVal3];[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation48

8、轉場動畫

  • CATransition 轉場動畫,是 CAAnimation 的子類,用于做轉場動畫,能夠為層提供移出屏幕和移入屏幕的動畫效果。UINavigationController 就是通過 CATransition 實現了將控制器的視圖推入屏幕的動畫效果。

  • 動畫屬性:

        type          :動畫類型。subtype       :動畫方向。startProgress :動畫起點(在整體動畫的百分比)。endProgress   :動畫終點(在整體動畫的百分比)。
  • 設置動畫類型

        基本型:kCATransitionFade      交叉淡化過渡kCATransitionPush      新視圖把舊視圖推出去kCATransitionMoveIn    新視圖移到舊視圖上面kCATransitionReveal    將舊視圖移開,顯示下面的新視圖用字符串表示的類型:fade   push   moveIn   reveal   和系統自帶的四種一樣pageCurl               向上翻頁效果pageUnCurl             向下翻頁效果rippleEffect           水滴效果suckEffect             收縮效果,如一塊布被抽走cube                   立方體翻滾效果alignedCube            立方體翻滾效果flip                   翻轉效果alignedFlip            翻轉效果oglFlip                翻轉效果rotate                 旋轉cameraIrisHollowOpen   相機鏡頭打開效果cameraIrisHollowClose  相機鏡頭關閉效果cameraIris             相機鏡頭打開關閉效果
    • kCATransitionFade 和 fade 效果,kCATransitionPush 和 push 效果

      animation50animation51

    • kCATransitionMoveIn 和 moveIn 效果,kCATransitionReveal 和 reveal 效果

      animation52animation53

    • pageCurl 效果,pageUnCurl 效果

      animation54animation55

    • rippleEffect 效果,suckEffect 效果

      animation56animation57

    • cube、alignedCube 效果,flip、alignedFlip、oglFlip 效果

      animation58animation59

    • rotate 效果,cameraIris 效果

      animation60animation61

    • cameraIrisHollowOpen 效果,cameraIrisHollowClose 效果

      animation62animation63

  • 設置動畫方向

        四種預設,某些類型中此設置無效:kCATransitionFromRightkCATransitionFromLeftkCATransitionFromTopkCATransitionFromBottom
  • UIView 實現轉場動畫方法

        // 單視圖+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;參數說明:view:需要進行轉場動畫的視圖duration:動畫的持續時間options:轉場動畫的類型animations:將改變視圖屬性的代碼放在這個 block 中completion:動畫結束后,會自動調用這個 block// 雙視圖+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;參數說明:duration:動畫的持續時間options:轉場動畫的類型completion:動畫結束后,會自動調用這個 block

8.1 添加轉場動畫

  • 添加轉場動畫

        @property (weak, nonatomic) IBOutlet UIView *animView;@property (weak, nonatomic) IBOutlet UIImageView *imageView;- (IBAction)btnClick:(id)sender {static int i = 2;// 加載圖片self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"page%d", i]];i = (i == 2) ? 1 : i + 1;// 添加轉場動畫CATransition *anim = [CATransition animation];// 設置動畫效果anim.type = @"flip";// 設置動畫方向anim.subtype = kCATransitionFromLeft;// 設置動畫時間anim.duration = 1.0;// 添加轉場動畫[self.animView.layer addAnimation:anim forKey:nil];}

    animation31

    • 效果

      animation49

9、動畫組

  • CAAnimationGroup 動畫組,是 CAAnimation 的子類,可以保存一組動畫對象,將 CAAnimationGroup 對象加入層后,組中所有動畫對象可以同時并發運行。

  • 屬性說明:

        animations :用來保存一組動畫對象的 NSArray。默認情況下,一組動畫對象是同時運行的,也可以通過設置動畫對象的 beginTime 屬性來更改動畫的開始時間。

9.1 添加動畫組

  • 添加動畫組

        // 同時旋轉,縮放,平移CAAnimationGroup *anim = [CAAnimationGroup animation];CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];rotation.toValue = @(M_PI_4 * 3);CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];scale.toValue = @0.5;CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"transform.translation"];translation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];// 添加動畫anim.animations = @[rotation, scale, translation];anim.duration = 3.0;anim.removedOnCompletion = NO;anim.fillMode = kCAFillModeForwards;// 添加動畫組[self.myView.layer addAnimation:anim forKey:nil];
    • 效果

      animation64

10、核心動畫的使用

  • 具體講解見 iOS - Core Animation 核心動畫的使用

轉載于:https://www.cnblogs.com/QianChia/p/6359714.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/457258.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/457258.shtml
英文地址,請注明出處:http://en.pswp.cn/news/457258.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

navicat 官方使用手冊,中文版,快捷鍵大全

2017年1月23日09:52:51 這個官方中文文檔很詳細 https://www.navicat.com.cn/manual/online_manual/cn/navicat/win_manual/index.html https://community.navicat.com/videos/cn 官方中文論壇部分視頻教程 快捷鍵 Navicat 主窗口 鍵動作CTRLG設置位置文件夾CTRL#&#xff08;#…

Linux inode與文件系統關系

inode只有在linux文件系統的概念&#xff08;ext3,ext4) 、inode節點數量與文件存儲的關系。 二、在文件系統初始化時設置合適的節點數量。 linux服務器在存儲文件小而數量多的情況下&#xff0c;需要考慮inode用完的情況。轉載于:https://www.cnblogs.com/lirunzhou/p/5883706…

評分系統 java_C自動評分系統

我無法按照規范完成作業 . 這是分配方案&#xff1a;大學迫切需要一個自動測試評分系統 . 使用C&#xff0c;為大學寫一個評分系統&#xff0c;并對至少五名學生的測試進行評分 . 要創建評分系統&#xff0c;請按照以下步驟操作&#xff1a;首先詢問測試中的問題數量然后詢問每…

當Terraform遇上ECS(一)——DataSource篇

背景 越來越多的公司已經熟知并運用“基礎設施即代碼”來構建和維護自己的云基礎設施。目前也有許多的自動化構建工具協助用戶通過腳本進行云資源的部署和生命周期的管理&#xff0c;如&#xff1a;Terraform、Ansible、Chef等。但是&#xff0c;在實施過程中&#xff0c;都遇到…

【BZOJ 1597】 [Usaco2008 Mar]土地購買 (斜率優化)

1597: [Usaco2008 Mar]土地購買 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3601 Solved: 1322Description 農夫John準備擴大他的農場,他正在考慮N (1 < N < 50,000) 塊長方形的土地. 每塊土地的長寬滿足(1 < 寬 < 1,000,000; 1 < 長 < 1,000,000). …

深入淺出學java_《深入淺出學JAVA開發初級》

整體說明&#xff1a;Java私塾的這一套視頻是完全真實課堂錄制&#xff0c;實際上課時間為十一天&#xff0c;主要內容包括&#xff1a;1&#xff1a;系統完整的學習Java的基礎知識2&#xff1a;深入剖析重點知識點的理論3&#xff1a;超多的編程題目和程序講解4&#xff1a;最…

重定位與鏈接腳本

1.為什么需要重定位   位置無關編碼(PIC&#xff0c;position independent code)&#xff1a;匯編源文件被編碼成二進制可執行程序時編碼方式與位置&#xff08;內存地址&#xff09;無關。  位置有關編碼&#xff1a;匯編源碼編碼成二進制可執行程序后和內存地址是有關的。…

Linux bashrc和profile的用途和區別

導讀使用終端ssh登錄Linux操作系統的控制臺后&#xff0c;會出現一個提示符號&#xff08;例如&#xff1a;#或~&#xff09;&#xff0c;在這個提示符號之后可以輸入命令&#xff0c;Linux根據輸入的命令會做回應&#xff0c;這一連串的動作是由一個所謂的Shell來做處理。Shel…

python讀取word文檔結構圖_Word 有什么技巧,讓你相見恨晚?

Word作為日常辦公最常用的軟件之一&#xff0c;其實真沒你想得那么簡單&#xff01;你不知道的每一個技巧&#xff0c;都會讓你相見恨晚&#xff01;每當身邊的小伙伴詢問這些疑難雜癥時&#xff0c;我都會拋出這張圖…真的沒騙你&#xff0c;我們遇到的 99% 的Word難題&#x…

Golang 特性簡介

by sheepbao 主要大概介紹go語言的歷史和特性&#xff0c;簡單的入門。 來歷 很久以前&#xff0c;有一個IT公司&#xff0c;這公司有個傳統&#xff0c;允許員工擁有20%自由時間來開發實驗性項目。在2007的某一天&#xff0c;公司的幾個大牛&#xff0c;正在用c開發一些比較繁…

HTML實體字符轉化為HTML標簽

html_entity_decode方法 參數描述string必需。規定要解碼的字符串。flags 可選。規定如何處理引號以及使用哪種文檔類型。 可用的引號類型&#xff1a; ENT_COMPAT - 默認。僅解碼雙引號。ENT_QUOTES - 解碼雙引號和單引號。ENT_NOQUOTES - 不解碼任何引號。規定所使用文檔類型…

華為2017java筆試題_2017年java華為面試題

2017年java華為面試題通過HCNP認證&#xff0c;將證明您對中小型網絡有全面深入的了解&#xff0c;掌握中小型網絡的通用技術&#xff0c;并具備獨立設計中小型網絡以及使用華為路由交換設備實施設計的能力。下面是小編收集的關于java華為面試題&#xff0c;希望大家認真閱讀!1…

Tomcat 配置詳解/優化方案

Server.xml 【原地址&#xff1a;http://blog.csdn.net/cicada688/article/details/14451541】 Server.xml配置文件用于對整個容器進行相關的配置。 <Server>元素&#xff1a;是整個配置文件的根元素。表示整個Catalina容器。 屬性&#xff1a;className&#xff1a;實現…

MySQL創建數據庫與創建用戶以及授權

1、create schema [數據庫名稱] default character set utf8 collate utf8_general_ci;--創建數據庫 采用create schema和create database創建數據庫的效果一樣。 2、create user [用戶名稱]% identified by [用戶密碼];--創建用戶 密碼8位以上&#xff0c;包括&#xff1a;大寫…

java 防止url重復請求_Web項目如何防止客戶端重復發送請求

在Web項目中&#xff0c;有一些請求或操作會對數據產生影響(比如新增、刪除、更新)&#xff0c;針對這類請求一般都需要做一些保護&#xff0c;以防止用戶有意或無意的重復發起這樣的請求導致的數據錯亂。本文總結了一些防止客戶端重復發送請求的方法。方法一&#xff1a;JS監聽…

【bzoj1010-toy】斜率優化入門模板

dsy1010: [HNOI2008]玩具裝箱 【題目描述】 有n個數&#xff0c;分成連續的若干段&#xff0c;每段&#xff08;假設從第j個到第i個組成一段&#xff09;的分數為 (X-L)^2&#xff0c;X為j-iSigma(Ck) i<k<j&#xff0c;其中L是一個常量。目標&#xff1a;各段分數的總和…

itellyou操作系統,office等軟件的很全的下載站

itellyou操作系統&#xff0c;office等軟件的很全的下載站http://www.itellyou.cn/轉載于:https://blog.51cto.com/wangheyu1/1894724

矩陣的馬鞍點

#include<stdio.h>#define n 4//馬鞍點是第I行值最小第J列值最大 void maxmin(int a[n][n]){ int i,j ,flag; int max[n],min[n]; for(i0;i<n;i) { min[i]a[i][0];//將數組每行的第一個元素賦值給min[]數組 for(j1;j<n;j) { if(a[i][j]<min[i]) min[i]a[i][j];…

Linux運維工程師面試-部分題庫

一、Linux操作系統知識 1.常見的Linux發行版本都有什么&#xff1f;你最擅長哪一個&#xff1f;它的官網網站是什么&#xff1f;說明你擅長哪一塊&#xff1f; 2.Linux開機啟動流程詳細步驟是什么&#xff1f;系統安裝完&#xff0c;忘記密碼如何破解&#xff1f; 3.企業中Linu…