在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
下面是能夠設置動畫效果的屬性:函數
例如一個視圖淡出屏幕,另一個視圖出現的代碼:動畫
[UIView animateWithDuration:1.0 animations:^{ firstView.alpha = 0.0; secondView.alpha = 1.0; }];
如何實現連續的動畫?
能夠在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