iOS - 在UIView的animateWithDuration完成块中,回调函数会过早地被调用。

32 浏览
0 Comments

iOS - 在UIView的animateWithDuration完成块中,回调函数会过早地被调用。

当一个表格视图单元格被选中时,我正在尝试做一些动画。出现了一些问题,完成块提前被调用。即使将持续时间设置为10秒,完成块也会立即被调用。

[UIView animateWithDuration:10.0 animations:^{
    message.frame = newFrame;
} completion:^(BOOL finished) {
    NSLog(@"DONE???");
}];

有什么想法是为什么会发生这种情况吗?谢谢。

admin 更改状态以发布 2023年5月25日
0
0 Comments

是的。它被过早地调用了,因为它被以某种方式中断了。可能是由于模态演示过渡或其他原因。根据您的需求,以下可能是您喜欢的解决方案。我们通过手动延迟我们的动画代码的执行来避免冲突,如下所示:

// To get this in Xcode very easily start typing, "dispatch_aft..."
// Note the "0.2". This ensures the outstanding animation gets completed before we start ours
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [UIView animateWithDuration:1.0 delay:0 options:0 animations:^{
        // Your animation code
    } completion:^(BOOL finished) {
        // Your completion code
    }];
});

0
0 Comments

来自UIView文档

完成

动画序列结束时要执行的块对象。此块没有返回值,而且有一个布尔参数,指示动画是否在调用完成处理程序之前实际完成。如果动画的持续时间为0,则在下一个运行循环周期的开头执行此块。此参数可以为NULL。

这意味着不能保证代码仅在动画完成后执行。我建议你按照“完成”参数作为执行条件进行检查。

0