【问题标题】:How do I get an UIImageView to tell me when its animation stops?如何让 UIImageView 告诉我它的动画何时停止?
【发布时间】:2026-01-30 11:10:02
【问题描述】:

我有一个 UIImageView 将一系列图片显示为动画。这部分项目运行良好。

我想在动画结束时显示一个标签。 Imageview 动画结束时是否会触发事件?

【问题讨论】:

    标签: iphone objective-c animation uiimageview


    【解决方案1】:

    使用setAnimationDuration:设置动画的持续时间 同时你配置performSelector:withObject:withDelay 的延迟与动画的持续时间相同

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0]; //the animation will last for 1.0s
    //some animation code here
    [UIView commitAnimations];
    [self performSelector:@selector(someMethodToDisplayLabel) withObject:nil afterDelay:1.0];
        //someMethodToDisplayLabel will be called after 1.0s
    

    【讨论】:

    • 漂亮!正是我想要的。
    • 好吧,UIImageView 有自己的 startAnimating 和 stopAnimating 方法来做动画。我认为克里斯正在使用但不调用 UIView 动画方法。我使用了一些KVO技巧来尝试听“isAnimating”或“animating”的值,它不起作用。由于 UIImageView 具有 animationDuration 和 animationRepeatCount 属性,我认为可以估计动画何时停止(持续时间 * 重复次数)。
    • 是的,我的回答建议使用这些持续时间和重复计数属性来确定何时开始您自己的动画。
    【解决方案2】:

    似乎没有为此举办活动/代表。我的第一个直觉是自己计算动画的长度,然后设置一个 NSTimer,这样当动画结束时,NSTimer 就会触发显示你接下来要显示的任何内容。

    【讨论】:

      最近更新 更多