From:http://www.codeios.com/thread-1038-1-1.html
这两天在写代码,发现原来3.1.3下经常用的uiview动画结束后调用事件的方法居然不起作用了,摸索了一下,发现ios4下对uivew的animation处理已经有新的方式了,和大家分享一下:
ios4以前的用法:
实现把myview向下移出屏幕后,再remove掉。
- [UIView beginAnimations:@"View Remove" context:nil];
- UIView setAnimationDelegate:self];
- [UIView setAnimationDidStopSelector:@selector(animationFinish:animationID:finished:context:)];
- [UIView setAnimationDuration:0.3f];
- [UIView setAnimationCurve:UIViewAnimationCurveLinear];
- myView.center = CGPointMake(myView.center.x, myView.center.y+480.0);
- UIView commitAnimations];
- - (void)animationFinish:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
- if (animationID == @"View Remove") {
- [myView removeFromSuperview];
- }
- }
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear
- animations:^{
- settingView.center = CGPointMake(myView.center.x, myView.center.y+480.0);
- }
- completion:^(BOOL finished){
- if (finished){
- [myView removeFromSuperview];
- }
- }
- ];
- animateWithDuration:animations:
- + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
duration
The total duration of the animations, measured in seconds. If you specify a negative value or 0, the changes are made without animating them.
animations
A block object containing the changes to commit to the views. This is where you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL.
Discussion
This method performs the specified animations immediately using the default animation options. The default options are UIViewAnimationOptionCurveEaseInOut and UIViewAnimationOptionTransitionNone.
Availability
Available in iPhone OS 4.0 and later.
Declared In
UIView.h
- animateWithDuration:animations:completion:
- + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
duration
The total duration of the animations, measured in seconds. If you specify a negative value or 0, the changes are made without animating them.
animations
A block object containing the changes to commit to the views. This is where you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL.
completion
A block object to be executed when the animation sequence ends. This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called. If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle. This parameter may be NULL.
Discussion
This method performs the specified animations immediately using the default animation options. The default options are UIViewAnimationOptionCurveEaseInOut and UIViewAnimationOptionTransitionNone.
For example, if you want to fade a view until it is totally transparent and then remove it from your view hierarchy, you could use code similar to the following:
- [UIView animateWithDuration:0.2
- animations:^{ view.alpha = 0.0; }
- completion:^(BOOL finished){ [view removeFromSuperview]; }]
Available in iPhone OS 4.0 and later.
Declared In
UIView.h
- animateWithDuration:delay:options:animations:completion:
- + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
duration
The total duration of the animations, measured in seconds. If you specify a negative value or 0, the changes are made without animating them.
delay
The amount of time (measured in seconds) to wait before beginning the animations. Specify a value of 0 to begin the animations immediately.
options
A mask of options indicating how you want to perform the animations. For a list of valid constants, see UIViewAnimationOptions.
animations
A block object containing the changes to commit to the views. This is where you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL.
completion
A block object to be executed when the animation sequence ends. This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called. If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle. This parameter may be NULL.
Discussion
This method initiates a set of animations to perform on the view. The block object in the animations parameter contains the code for animating the properties of one or more views.
Availability
Available in iPhone OS 4.0 and later.
Declared In
UIView.h
本文介绍从 iOS 4 开始 UIView 动画的新变化,包括弃用旧的动画结束回调方法并推荐使用基于 block 的新方法。文中详细对比了两种不同实现方式,并提供了代码示例。
5686

被折叠的 条评论
为什么被折叠?



