简体   繁体   English

iOS - UIView animateWithDuration 中的完成块被过早调用

[英]iOS - completion block in UIView animateWithDuration gets called too early

I'm trying to do some animation when a table view cell gets selected.当表视图单元格被选中时,我正在尝试做一些动画。 For some reason, the completion block is getting called way too early.出于某种原因,完成块被调用得太早了。 Even setting the duration to 10 seconds, the completion block gets called immediately.即使将持续时间设置为 10 秒,完成块也会立即被调用。

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

Any thoughts on why this is happening?关于为什么会发生这种情况的任何想法? Thanks.谢谢。

From the UIView documentation :UIView 文档

completion完成

A block object to be executed when the animation sequence ends.动画序列结束时要执行的块对象。 This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called.这个块没有返回值,并接受一个布尔参数,指示在调用完成处理程序之前动画是否实际完成。 If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle.如果动画的持续时间为 0,则在下一个 run loop 循环开始时执行此块。 This parameter may be NULL.此参数可能为 NULL。

What this means is that there isn't a guarantee that the code will be executed only when the animation is done.这意味着不能保证代码仅在动画完成时执行。 I'd advise you to check the "finished" parameter as a condition for execution.我建议您检查“完成”参数作为执行条件。

Yes.是的。 It is being called too early because it's being interrupted somehow.它被调用得太早了,因为它以某种方式被打断了。 Probably by a modal presentation transition or perhaps something else.可能通过模态演示过渡或其他方式。 Depending on your needs, the following may be a solution you like.根据您的需求,以下可能是您喜欢的解决方案。 We avoid the conflict by manually delaying the execution of our animation code like so:我们通过手动延迟动画代码的执行来避免冲突,如下所示:

// 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
    }];
});

如果动画没有效果,也可以提前调用完成,例如将视图的 alpha 设置为它已有的值。

Posting this here in case it helps.如果有帮助,请在此处发布。 In my case I was using a keyframe animation and the completion block was called immediately.就我而言,我使用的是keyframe动画,并且立即调用了完成块。 Beware these concepts:注意这些概念:

  • duration: The duration of the overall animation, measured in seconds.持续时间:整个动画的持续时间,以秒为单位。 If you specify a negative value or 0, changes are made immediately and without animations.如果您指定一个负值或 0,则会立即进行更改并且没有动画。

  • frameStartTime: The time at which to start the specified animations. frameStartTime:开始指定动画的时间。 This value must be in the range 0 to 1, where 0 represents the start of the overall animation and 1 represents the end of the overall animation.该值必须在 0 到 1 的范围内,其中 0 表示整个动画的开始,1 表示整个动画的结束。 For example, for an animation that is two seconds in duration, specifying a start time of 0.5 causes the animations to begin executing one second after the start of the overall animation.例如,对于持续时间为两秒的动画,将开始时间指定为 0.5 会导致动画在整个动画开始后一秒开始执行。

  • frameDuration: The length of time over which to animate to the specified value. frameDuration:动画到指定值的时间长度。 This value must be in the range 0 to 1 and indicates the amount of time relative to the overall animation length.该值必须在 0 到 1 的范围内,并指示相对于整个动画长度的时间量。 If you specify a value of 0, any properties you set in the animations block update immediately at the specified start time.如果您指定的值为 0,则您在动画块中设置的任何属性都会在指定的开始时间立即更新。 If you specify a nonzero value, the properties animate over that amount of time.如果您指定一个非零值,则属性会在该时间段内进行动画处理。 For example, for an animation that is two seconds in duration, specifying a duration of 0.5 results in an animation duration of one second.例如,对于持续时间为两秒的动画,指定持续时间为 0.5 会导致动画持续时间为一秒。

Setting any of these values wrong will interfere with the time the completion gets called.将这些值中的任何一个设置错误都会干扰完成被调用的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 强制调用UIView animateWithDuration完成块 - Force UIView animateWithDuration completion block to be called ios - UIView.animateWithDuration 的交互式过渡完成块从未在 animateTransition 中调用 - ios - interactive transition completion block for UIView.animateWithDuration never called inside animateTransition 在animateWithDuration:completion的完成块中删除UIView - Remove UIView inside the completion block of animateWithDuration:completion 了解[UIView animateWithDuration:completion:]的完成块 - Understanding the completion block of [UIView animateWithDuration:completion:] UIView animateWithDuration完成块被循环中断中断; - UIView animateWithDuration completion block interrupted by loop break; UIView AnimateWithDuration从不到达完成块 - UIView AnimateWithDuration Never Reaches Completion Block 在 UIView.animateWithDuration 的完成块中添加视图 - Adding a view in a completion block of UIView.animateWithDuration Swift UIView animateWithDuration完成闭包立即调用 - Swift UIView animateWithDuration completion closure called immediately UIView 动画的完成块在动画完成之前被调用 - The completion block of UIView animation gets called before the animation is finished 复制块动画ios [UIView animateWithDuration:…] - Replicate block animation ios [UIView animateWithDuration:…]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM