Swift UIView animateWithDuration completion closure called immediately

孤人 提交于 2020-01-23 05:04:26

问题


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.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.


回答1:


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. 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.

// 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()
         }
    })
})



回答2:


I resolved this in the end by moving the animation from hitTest() and into touchesBegan() in the UIView



来源:https://stackoverflow.com/questions/25626515/swift-uiview-animatewithduration-completion-closure-called-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!