animateWithDuration

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

函數原型:ios

+ (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

其中,app

  • 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代碼塊中添加動畫。
    下面是實例代碼:ui

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

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

相關文章
相關標籤/搜索