iOS動畫1 — UIView動畫

來源:互聯網
上載者:User

標籤:

  iOS動畫基礎是Core Animation核心動畫。Core Animation是iOS平台上負責圖形渲染與動畫的基礎設施。由於核心動畫的實現比較複雜,蘋果提供了實現簡單動畫的介面—UIView動畫。UIView動畫封裝在UIView的Category中,主要實現一些簡單和常用的動畫。UIView動畫是對核心動畫進行了一層封裝,所以最終動畫還是通過Core Animation的介面實現。

 

                         

  

  主要的動畫效果都可以通過UIView動畫和Core Animation實現,在技術上如何選擇呢?

  1、簡單的、只執行一次的動畫使用UIView動畫,如頁面切換。UIView動畫效能上比核心動畫差,如果動畫簡單的話效能損失可以忽略不計,而UIView使用起來很方便。

  2、多次或無限次重複的動畫使用Core Animation。這個時候使用UIView動畫會有效能問題。

    3、UIView實現不了的動畫。UIView對Core Animation的常用動畫進行了封裝,不能解決所有的問題。

 

  UIView動畫有兩種調用方式

 

  不推薦使用下面這種動畫塊的方式。

1234 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0]; self.view.alpha = 0.0;[UIView commitAnimations];

  

  推薦使用帶block的方法

123 [UIView animateWithDuration:1.0 animations:^{ } completion:^(BOOL finshed){  }]; 

  

  下面三個類方法其實是一個方法,方法2和3省略了方法1的一些參數。

1 + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

  

1 + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

 

1 + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

  duration    動畫持續的時間

  delay        動畫延遲的時間

  options       如何執行動畫的一些選項,比如先快後慢的執行動畫。

  animations  動畫block,在這裡對要進行動畫變換的View進行形變、位移、旋轉、漸層等操作

  completion    動畫完成後調用的block

 

  [內容] 檢視動畫,這個參數view的所有subview都將跟隨這個view一起變化。

1 + (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

  

  兩個view的過渡動畫,從fromView切換到toView。注意:這個動畫作用的不是fromView和toView本身,而是兩個view的共同的superview。

1 + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

 

  options定義:

123456789101112131415161718192021222324252627 enum {   UIViewAnimationOptionLayoutSubviews            = 1 <<  0,   UIViewAnimationOptionAllowUserInteraction      = 1 <<  1,   UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2,   UIViewAnimationOptionRepeat                    = 1 <<  3,   UIViewAnimationOptionAutoreverse               = 1 <<  4,   UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5,   UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6,   UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7,   UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8,   UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9,       UIViewAnimationOptionCurveEaseInOut            = 0 << 16,   UIViewAnimationOptionCurveEaseIn               = 1 << 16,   UIViewAnimationOptionCurveEaseOut              = 2 << 16,   UIViewAnimationOptionCurveLinear               = 3 << 16,       UIViewAnimationOptionTransitionNone            = 0 << 20,   UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,   UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,   UIViewAnimationOptionTransitionCurlUp          = 3 << 20,   UIViewAnimationOptionTransitionCurlDown        = 4 << 20,   UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,   UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,   UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,};typedef NSUInteger UIViewAnimationOptions;

  

  UIViewAnimationOptionLayoutSubviews            所有子view跟父view作為一個整體一起動畫。此方式為預設 

  UIViewAnimationOptionAllowUserInteraction      在動畫運行過程中接收使用者操作

  UIViewAnimationOptionBeginFromCurrentState 從當前的狀態開始執行動畫。設定  

 

iOS動畫1 — 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.