iOS學習筆記10-UIView動畫

來源:互聯網
上載者:User

標籤:pac   curl   begin   end   line   anim   use   action   image   

上次學習了iOS學習筆記09-核心動畫CoreAnimation,這次繼續學習動畫,上次使用的CoreAnimation很多人感覺使用起來很繁瑣,有沒有更加方便的動畫效果實現呢?答案是有的,那就是UIView動畫封裝

一、UIView動畫

蘋果知道圖層動畫使用麻煩,就為我們封裝到了UIView裡,使我們可以簡單的實現各種動畫效果。

1. 基礎動畫主要實現方法為:
+ (void)animateWithDuration:(NSTimeInterval)duration                       delay:(NSTimeInterval)delay                    options:(UIViewAnimationOptions)options                  animations:(void (^)(void))ainimations                  completion:(void (^)(BOOL finished))completion;
移動動畫使用執行個體:
/*   開始動畫,UIView的動畫方法執行完後動畫會停留在終點位置,而不需要進行任何特殊處理  duration:執行時間  delay:延遲時間  options:動畫設定,例如自動回復、勻速運動等  completion:動畫完成回調方法*/[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{    _imageView.center = location;} completion:^(BOOL finished) {    NSLog(@"Animation end.");}];

上面的方法基本滿足大部分基礎動畫的要求,還有一種比較有趣的動畫效果

彈簧動畫效果:
/*  建立彈性動畫  damping:阻尼,範圍0-1,阻尼越接近於0,彈性效果越明顯  springVelocity:彈性複位的速度*/[UIView animateWithDuration:5.0 delay:0 usingSpringWithDamping:0.1       initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{    _imageView.center = location;} completion:^(BOOL finished) {    NSLog(@"Animation end.");}];

UIView動畫方法中有個options參數,是枚舉類型,可以組合使用:

/* 常規動畫屬性設定,可以同時選擇多個,用或運算組合  */UIViewAnimationOptionLayoutSubviews/*< 動畫過程中保證子視圖跟隨運動 */          UIViewAnimationOptionAllowUserInteraction/*< 動畫過程中允許使用者互動 */UIViewAnimationOptionBeginFromCurrentState/*< 所有視圖從目前狀態開始運行 */UIViewAnimationOptionRepeat/*< 重複運行動畫 */                 UIViewAnimationOptionAutoreverse/*< 動畫運行結束後回到起始點 */             UIViewAnimationOptionOverrideInheritedDuration/*< 忽略嵌套動畫時間設定 */ UIViewAnimationOptionOverrideInheritedCurve/*< 忽略嵌套動畫速度設定 */    UIViewAnimationOptionAllowAnimatedContent/*< 動畫過程中重繪視圖,只適合轉場動畫 */    UIViewAnimationOptionShowHideTransitionViews/*< 視圖切換直接隱藏舊視圖、顯示新視圖,只適合轉場動畫 */   UIViewAnimationOptionOverrideInheritedOptions/*< 不繼承父動畫設定或動畫類型 */  /* 動畫速度變化控制,其中選一個進行設定 */    UIViewAnimationOptionCurveEaseInOut/*< 動畫先緩慢,後逐漸加速,預設值 */       UIViewAnimationOptionCurveEaseIn/*< 動畫逐漸層慢 */          UIViewAnimationOptionCurveEaseOut/*< 動畫逐漸加速 */             UIViewAnimationOptionCurveLinear/*< 動畫勻速執行 */            
2. 主要畫面格動畫主要實現方法為:
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration                                delay:(NSTimeInterval)delay                             options:(UIViewAnimationOptions)options                           animations:(void (^)(void))ainimations                           completion:(void (^)(BOOL finished))completion;
執行個體:
[UIView animateKeyframesWithDuration:5.0 delay:0         options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionCurveLinear animations:^{     //第一個主要畫面格:從0秒開始持續50%的時間,也就是5.0*0.5=2.5秒    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{        _imageView.center = location1;    }];    //第二個主要畫面格:從50%時間開始持續25%的時間,也就是5.0*0.25=1.25秒    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.25 animations:^{        _imageView.center = location2;    }];    //第三個主要畫面格:從75%時間開始持續25%的時間,也就是5.0*0.25=1.25秒    [UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{        _imageView.center = location3;    }];} completion:^(BOOL finished) {    NSLog(@"Animation end.");}];
主要畫面格動畫的options多了一些選擇:
/* 動畫模式選擇,選擇一個 */UIViewKeyframeAnimationOptionCalculationModeLinear/*< 連續運算模式 */UIViewKeyframeAnimationOptionCalculationModeDiscreter/*< 離散運算模式 */UIViewKeyframeAnimationOptionCalculationModePacedr/*< 均勻執行運算模式 */UIViewKeyframeAnimationOptionCalculationModeCubicr/*< 平滑運算模式 */UIViewKeyframeAnimationOptionCalculationModeCubicPacedr/*< 平滑均勻運算模式 */

UIView動畫現在只支援屬性主要畫面格動畫,不支援路徑主要畫面格動畫

3. 轉場動畫主要實現方法為:
+ (void)transitionWithView:(UIView *)view                   duration:(NSTimeInterval)duration                    options:(UIViewAnimationOptions)options                 animations:(void (^)(void))ainimations                 completion:(void (^)(BOOL finished))completion;
執行個體:
#pragma mark 轉場動畫- (void)transitionAnimation:(BOOL)isNext{    UIViewAnimationOptions option;    if (isNext) {        option = UIViewAnimationOptionCurveLinear | UIViewAnimationOptionTransitionFlipFromRight;    } else {        option = UIViewAnimationOptionCurveLinear | UIViewAnimationOptionTransitionFlipFromLeft;    }    [UIView transitionWithView:_imageView duration:1.0 options:option animations:^{        _imageView.image = [self getImage:isNext];    } completion:nil];}
轉場動畫options多了一些選擇:
/* 轉場類型 */UIViewAnimationOptionTransitionNone/*< 沒有轉場動畫效果 */UIViewAnimationOptionTransitionFlipFromLeft/*< 從左側翻轉效果 */UIViewAnimationOptionTransitionFlipFromRight/*< 從右側翻轉效果 */UIViewAnimationOptionTransitionCurlUp/*< 向後翻頁的動畫過渡效果 */  UIViewAnimationOptionTransitionCurlDown/*< 向前翻頁的動畫過渡效果 */   UIViewAnimationOptionTransitionCrossDissolve/*< 溶解消失效果 */ UIViewAnimationOptionTransitionFlipFromTop/*< 從上方翻轉效果 */UIViewAnimationOptionTransitionFlipFromBottom/*< 從底部翻轉效果 */
  • 使用UIView動畫封裝的轉場動畫效果少,這裡無法直接使用私人API
  • 兩個視圖之間轉場可以使用
    ```
  • (void)transitionFromView:(UIView )fromView
    toView:(UIView 
    )toView
    duration:(NSTimeInterval)duration
    options:(UIViewAnimationOptions)options
    completion:(void (^)(BOOL finished))completion;
    ```
  • 預設情況,轉出的視圖會從父視圖移除,轉入後重新添加
 

iOS學習筆記10-UIView動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.