ios - 在完成之前停止 animateWithDuration

标签 ios objective-c uiview core-animation

我在 UIViewController 中使用此方法来显示一条消息三秒钟,然后淡出。 mainMessageLabel 是在接口(interface)文件中声明的 UILabel

- (void) showTempMessage: (NSString*) message
{
    _mainMessageLabel.text = message;
    _mainMessageLabel.alpha = 1;

    [UIView animateWithDuration: 1
                          delay: 3
                        options: 0
                     animations: ^{
                         _mainMessageLabel.alpha = 0;
                     }
                     completion: ^(BOOL finished) {
                         _mainMessageLabel.text = @"";
                         _mainMessageLabel.alpha = 1;
                     }];
}

如果我在上一次调用后至少四秒调用它,该方法工作正常,但如果我更早调用它,当前一个实例仍在动画时,标签消失,我必须再等四秒钟才能调用它再次工作。每次调用此方法时,我都希望它停止之前的动画并显示新消息三秒钟,然后淡出。

我在这里尝试了其他问题的答案,比如在options:中添加UIViewAnimationOptionBeginFromCurrentState,甚至添加[_mainMessageLabel.layer removeAllAnimations] 到我函数的顶部,但没有任何效果。你有什么建议吗?

最佳答案

问题是完成 block 的时间(它在取消先前动画并重置标签的代码之后触发)。简单的解决方案是完全消除该完成 block (将它的 alpha 设为零与将 text 设置为 @"" 并使其“可见”没有区别)。因此:

_mainMessageLabel.text = message;
_mainMessageLabel.alpha = 1.0;

[UIView animateWithDuration:1 delay:3 options:0 animations:^{
    _mainMessageLabel.alpha = 0.0;
} completion:nil];

关于ios - 在完成之前停止 animateWithDuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25256872/

相关文章:

iOS 8、Xcode 6 标签栏图像色调

php - 我想使用 HpCloud for iphone sdk 的人脸检测 API

ios - enumerateObjectsUsingBlock 中的计数器

objective-c - 程序崩溃,需要帮助才能至少在哪里找到问题

ios - 如何改变CALayer动画的方向

uitableview - UITableView:以编程方式更改页脚 View 的大小不起作用

ios - 在 animateAlongsideTransition block 内的 UIImage 上进行交叉溶解

ios - 协议(protocol)和反射中的静态变量

ios - 如何引用类的方法

iOS 1 点 View 显示模糊