【问题标题】:iOS: How to detect when an animation is finished?iOS:如何检测动画何时完成?
【发布时间】:2011-12-08 01:10:47
【问题描述】:

有没有一种方法可以检测动画何时结束?动画完成后我想打电话给[nav setTitle:navItem]

下面是我的代码的 sn-p。希望问题足够清楚,所以我可以得到一个解决方案,最好是一个例子。

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

if(event.subtype == UIEventSubtypeMotionShake){


    NSString *imageUrl = @"";
    NSString *navItem = @"";

    int randomNumber = arc4random() % 3;

    switch (randomNumber) {
        case 0:
            imageUrl = @"pic1.png";
            navItem = @"txt1";
            break;
        case 1:
            imageUrl = @"pic2.png";
            navItem = @"txt2";
            break;
        case 2:
            imageUrl = @"pic3.png";
            navItem = @"txt3";
            break;
        default:
            break;
    }

    animation.animationImages = [NSArray arrayWithObjects:

                                 [UIImage imageNamed:@"pic1.png"],
                                 [UIImage imageNamed:@"pic3.png"],
                                 [UIImage imageNamed:@"pic2.png"],
                                 [UIImage imageNamed:@"pic1.png"],
                                 [UIImage imageNamed:@"pic2.png"],
                                 [UIImage imageNamed:@"pic3.png"], 
                                 [UIImage imageNamed:imageUrl],
                                 nil];

    UIImage *img = [UIImage imageNamed:imageUrl];
    [imageview setImage:img];

    [animation setAnimationRepeatCount:1];
    animation.animationDuration = 1;
    [animation startAnimating]; 

    [nav setTitle:navItem]; 

}

}

【问题讨论】:

  • 如果您找到解决问题的答案,最好接受它,您将获得 2 个声望分。

标签: iphone ios objective-c uiimageview


【解决方案1】:
    [self performSelector:@selector(animationFinished) withObject:nil afterDelay:1.0];

-(void)animationFinished {
    // do stuff
}

这只是假设动画将在您告诉它的时间内完成。

【讨论】:

    【解决方案2】:

    您可以使用 setAnimationDidStopSelector 委托。
    http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html

    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    
    
    - (void)animationDidStop:(NSString*)animationID finished:(BOOL)finished context:(void *)context {
        /*Do stuff*/
    }
    

    【讨论】:

    • 我不知道这是否适用于animationImages类型的动画。仅适用于使用 [UIView beginAnimations:context:] 的动画。
    【解决方案3】:

    动画在[animation isAnimating] == NO时结束。

    如果你想等到它完成但不阻塞 UI:

    while ([animation isAnimating]) {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2019-02-09
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2023-03-04
    • 2010-11-02
    相关资源
    最近更新 更多