You are encouraged to use the animatewithduration Method for animation effects in ios4.0 and later versions. Of course, the previous begin/commit method is still used. The following describes how to use animatewithduration in detail.
Function prototype:
+ (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
Where,
- Duration is the animation duration.
- Animations is the animation effect code block.
The following shows the attributes for setting the animation effect:
- Frame
- Bounds
- Center
- Transform
- Alpha
- Backgroundcolor
- Contentstretch
For example, if a view fades out of the screen and another view shows code:
[UIView animateWithDuration:1.0 animations:^{ firstView.alpha = 0.0; secondView.alpha = 1.0;}];
- Completion is the code block that is executed after the animation is executed.
- Options is the animation execution option. Refer to here
- Delay is the waiting time before the animation starts to run.
How to achieve continuous animation?
You can add animations to the completion code block.
The following is the instance code:
[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); }]; }];