如何中断UIView动画

时间:2017-11-12 23:14:18

标签: ios swift uiview uiviewanimation

在下面的代码中,我有一个视图,它显示两秒钟的信息,然后它会动画并消失。我希望能够做的是让用户能够通过在动画完成之前滑动视图来关闭视图。我的问题是,当动画正在进行时,视图无法识别手势。

如何在动画进行过程中让视图识别滑动手势?

override func viewDidAppear(_ animated: Bool) {
    closeMyViewWithAnimation()
}

func closeMyViewWithAnimation(){
    UIView.animate(withDuration: 0.3, delay: 2, options: .curveEaseIn, animations: {
        self.myView.frame.origin.y = 0 - self.myView.frame.height
    }, completion:{ finished in
        self.dismiss(animated: true, completion: nil)
    })
}

/// This gestures is not recognized while the closeMyViewWithAnimation
/// is in progress. How can I make the view to recognize this gesture?
@IBAction func closeMyViewWithGesture(_ sender: Any) {

    viewAddToCartAlert.layer.removeAllAnimations()

    UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseIn, animations: {
        self.myView.frame.origin.y = 0 - self.myView.frame.height
    }, completion:{ finished in
        self.dismiss(animated: true, completion: nil)
    })
}

编辑:

我最终使用计时器来延迟viewDidAppear中的动画,因此动画不会立即开始,允许用户滑动。

override func viewDidAppear(_ animated: Bool) {
    myTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(closeMyViewWithAnimation), userInfo: nil, repeats: false)
}

0 个答案:

没有答案