现在的位置: 首页 > 综合 > 正文

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

2018年02月14日 ⁄ 综合 ⁄ 共 4944字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.