开发者

How to let two animations for UIView executed one by one

I have a small UIView and want to let it move to center of screen first, and then, zoom to full screen. But when I begin start an animation like

[UIView beginAnim开发者_JS百科ations:@"animation1" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(showDetailToFullscreen)];
self.currentDetailVC.frame = centerFrame;
[UIView commitAnimations];

And in the same controller, I have a method:showDetailToFullscreen

- (void)showDetailToFullscreen {
    CGRect screenFrame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    [UIView beginAnimations:@"animation2" context:nil];
    [UIView setAnimationDuration:0.5];
    self.currentDetailVC.view.frame = screenFrame;
    [UIView commitAnimations];
}

but when start, it still executed together. I think the problem they are still in the same transaction. But how can I let these two animation executed one by one? Appreciate for any answer!


Try this instead of the setAnimationDidStopSelector:

[self performSelector:@selector(showDetailToFullscreen) withObject:nil afterDelay:0.5];


It's now considered much better to annimate views using the 'blocks' methods that can be found in the "Animating Views with Blocks" section in the UIView Class Reference.

In most of these methods you can specify a "completion" block of code that can be used to start off a second animation when the first animation completes. You could also call showDetailToFullscreen within this completion block.

I'd reccommend trying this method instead.


Are you targetting iOS 4 or later? If so, I recommend using the block-based animation methods instead. What you want to do is very easy:

Example from UIView docs:

[UIView animateWithDuration:0.2
     animations:^{view.alpha = 0.0;}
     completion:^(BOOL finished){ [view removeFromSuperview]; }];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜