简体   繁体   English

Swift UIView animateWithDuration完成闭包立即调用

[英]Swift UIView animateWithDuration completion closure called immediately

I'm expecting the completion closure on this UIView animation to be called after the specified duration, however it appears to be firing immediately... 我期望在指定的持续时间之后调用此UIView动画的完成闭包,但它似乎立即触发...

 UIView.animateWithDuration(
        Double(0.2),
        animations: {
            self.frame = CGRectMake(0, -self.bounds.height, self.bounds.width, self.bounds.height)
        },
        completion: { finished in
            if(finished) {
                self.removeFromSuperview()
            }
        }
    )

Has anyone else experienced this? 还有其他人经历过这个吗? I've read that others had more success using the center rather than the frame to move the view, however I had the same problems with this method too. 我已经读过其他人使用中心而不是框架来移动视图更成功,但是我也遇到了同样的问题。

For anyone else that is having a problem with this, if anything is interrupting the animation, the completion closure is immediately called. 对于遇到此问题的任何其他人,如果有任何事情正在中断动画,则立即调用完成闭包。 In my case, this was due to a slight overlap with a modal transition of the view controller that the custom segue was unwinding from. 在我的情况下,这是由于与自定义segue展开的视图控制器的模态转换略有重叠。 Using the delay portion of UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations:{} had no effect for me. I ended up using GCD to delay animation a fraction of a second. 使用UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations:{}delay部分UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations:{}对我没有任何影响。我最终使用GCD将动画延迟了几分之一秒。

// To avoid overlapping with the modal transiton
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {

    // Animate the transition
    UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {

         // Animations

         }, completion: { finished in

         // remove the views
         if finished {            
             blurView.removeFromSuperview()
             snapshot.removeFromSuperview()
         }
    })
})

我最终通过将动画从hitTest()移动到UIView touchesBegan()来解决这个问题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM