UIView animateWithDuration 使用詳解

在ios4.0及以後鼓勵使用animateWithDuration方法來實現動畫效果。當然,以往的begin/commit的方法依然使用,下面詳細解釋一下animateWithDuration的使用方法。

函數原型:

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

其中,

  • duration爲動畫持續的時間。
  • animations爲動畫效果的代碼塊。

下面是可以設置動畫效果的屬性:

frame bounds center transform alpha backgroundColor contentStretch

例如一個視圖淡出屏幕,另外一個視圖出現的代碼:

[UIView animateWithDuration:1.0 animations:^{ firstView.alpha = 0.0;
        secondView.alpha = 1.0; }];
  • completion爲動畫執行完畢以後執行的代碼塊
  • options爲動畫執行的選項。可以參考這裏
  • delay爲動畫開始執行前等待的時間

如何實現連續的動畫?
可以在completion代碼塊中添加動畫。
下面是實例代碼:

[UIView animateWithDuration:2.0 animations:^{ oldImageView.alpha = 0.0;
                     newImageView.alpha = 1.0; //imageView.center = CGPointMake(500.0, 512.0); } completion:^(BOOL finished){ [UIView animateWithDuration:4.0 animations:^{ newImageView.center = CGPointMake(500.0, 512.0); }]; }];

具體效果就是一幅圖片漸漸消失,另一幅圖片出現,然後圖片的位置移動。


發佈了16 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章