Swift UIView animateWithDuration完成闭包立即调用

Dav*_*dge 11 core-animation uiview ios swift

我希望在指定的持续时间之后调用此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()
            }
        }
    )
Run Code Online (Sandbox Code Playgroud)

还有其他人经历过这个吗?我已经读过其他人使用中心而不是框架移动视图更成功,但是我也遇到了同样的问题.

小智 10

对于遇到此问题的任何其他人,如果有任何事情正在中断动画,则立即调用完成闭包.在我的情况下,这是由于与自定义segue展开的视图控制器的模态转换略有重叠.使用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()
         }
    })
})
Run Code Online (Sandbox Code Playgroud)


小智 0

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