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);
                                      }];
                 }];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM