iOS4下实现UIView动画结束后调用事件的新方法[转载]

本文介绍从 iOS 4 开始 UIView 动画的新变化,包括弃用旧的动画结束回调方法并推荐使用基于 block 的新方法。文中详细对比了两种不同实现方式,并提供了代码示例。

From:http://www.codeios.com/thread-1038-1-1.html

 

这两天在写代码,发现原来3.1.3下经常用的uiview动画结束后调用事件的方法居然不起作用了,摸索了一下,发现ios4下对uivew的animation处理已经有新的方式了,和大家分享一下:
ios4以前的用法:
实现把myview向下移出屏幕后,再remove掉。

  1. [UIView beginAnimations:@"View Remove" context:nil];
  2. UIView setAnimationDelegate:self];
  3. [UIView setAnimationDidStopSelector:@selector(animationFinish:animationID:finished:context:)];
  4. [UIView setAnimationDuration:0.3f];
  5. [UIView    setAnimationCurve:UIViewAnimationCurveLinear];
  6. myView.center = CGPointMake(myView.center.x, myView.center.y+480.0);
  7. UIView    commitAnimations];
  8. - (void)animationFinish:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
  9.     if (animationID == @"View Remove") {
  10.         [myView removeFromSuperview];
  11.     }
  12. }
复制代码
Xcode的document已经明确说了setAnimationDidStopSelector :”Use of this method is discouraged in iPhone OS 4.0 and later. You should use the block-based animation methods instead.“,看来这种方法是行不通了,反正在我的模拟器上是不行了,以下是ios4中的实现方法:
  1. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear 
  2.         animations:^{
  3.         settingView.center = CGPointMake(myView.center.x, myView.center.y+480.0);
  4.         }
  5.         completion:^(BOOL finished){
  6.             if (finished){
  7.                 [myView removeFromSuperview];
  8.             }
  9.         }
  10.      ];
复制代码
更加简洁方便了,下面再附上两个相关实现方法的说明:
  1. animateWithDuration:animations:
复制代码
Animate changes to one or more views using the specified duration.
  1. + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
复制代码
Parameters
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
  1. animateWithDuration:animations:completion:
复制代码
Animate changes to one or more views using the specified duration and completion handler.
  1. + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
复制代码
Parameters
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:
  1. [UIView animateWithDuration:0.2
  2.            animations:^{ view.alpha = 0.0; }
  3.            completion:^(BOOL finished){ [view removeFromSuperview]; }]
复制代码
Availability
Available in iPhone OS 4.0 and later.
Declared In
UIView.h
  1. animateWithDuration:delay:options:animations:completion:
复制代码
Animate changes to one or more views using the specified duration, delay, options, and completion handler.
  1. + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
复制代码
Parameters
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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值