首页 文章

Swift UIView animateWithDuration完成闭包立即调用

提问于
浏览
11

我期望在指定的持续时间之后调用此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()
            }
        }
    )

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

2 回答

  • 0

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

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

相关问题