UIView动画完成后执行代码?

时间:2011-02-03 02:52:15

标签: iphone objective-c uiview

动画结束后执行代码的最佳方法是什么(例如,在淡出后从超视图中删除视图)?我看到setAnimationDidStopSelector:但不确定如何使用它。

2 个答案:

答案 0 :(得分:13)

这很简单。你必须定义像

这样的方法
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
//do smth
}

然后致电

[UIView beginAnimations: nil context: nil];
[UIView setAnimationDelegate: self]; //or some other object that has necessary method
[UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];

您可以发布要删除的视图。为此你必须这样做:

[UIView beginAnimations: nil context: someView];

然后添加回调:

   -(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
        [(UIView*)context removeFromSuperView];
    }

答案 1 :(得分:0)

我这样做的标签我想翻转。它启动一个动画但后来想在第一个完成后启动另一个动画。对于你的问题,你可以释放并释放不再需要的东西,而不是开始另一个动画。

- (void) doIt {
    CGRect  r = self.bounds;

    // Save some context information for after the animation
    struct contextStruct *c;
    c = (struct contextStruct *) malloc( sizeof(struct contextStruct) );
    c->oldHeight = r.size.height;

    [UILabelThatFlips beginAnimations: @"flipMe" context: c];
    [UILabelThatFlips setAnimationDelegate: self];
    [UILabelThatFlips setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
    [UILabelThatFlips setAnimationDuration: 0.3];
    [UILabelThatFlips setAnimationCurve: UIViewAnimationCurveEaseIn];
    if( animation == UILabelAsButtonAnimationFlip ) {
    r.size.height = 0.0;
    self.bounds = r;
    [UILabelThatFlips commitAnimations];
}

- (void) animationDidStop: (NSString *) animationID finished: (NSNumber *) finished context: (void *) context {
    struct contextStruct *c = context;
    CGRect  r = self.bounds;

    [UILabelThatFlips beginAnimations: @"flipBack" context: NULL];
    [UILabelThatFlips setAnimationDelay: 0.08];
    [UILabelThatFlips setAnimationDuration: 0.3];
    [UILabelThatFlips setAnimationCurve: UIViewAnimationCurveEaseOut];
    r.size.height = c->oldHeight;
    self.bounds = r;
    [UILabelThatFlips commitAnimations];

    // Stop up any memory leaks
    free( context );
}
相关问题