UIPanGestureRecognizer等待动画以.end状态结束,直到动画结束才触发.began

时间:2019-08-13 07:53:52

标签: swift

如果我在动画以.end状态结束之前进行互动,我想取消.end内部的所有动画。

func removeAllAnimations() {
    // The superview is the deck view that contains all of our cards here.
    superview?.subviews.forEach({ (subView: UIView) in
        // subView should be the CardViews.
        print("Removing all animations.")
        subView.layer.removeAllAnimations()
    })
}

@objc private func handlePan(gesture: UIPanGestureRecognizer) {
    switch gesture.state {
    case .began:
        print(".began")
        removeAllAnimations()
    case .changed:
        print(".changed")
        handleChanged(gesture: gesture)
    case .ended:
        print(".ended")
        handleEnded(gesture)
    default:
        break;
    }
}

private func handleEnded(_ gesture: UIPanGestureRecognizer) {
    let translationDirection: CGFloat = gesture.translation(in: nil).x > 0 ? 1 : -1
    let shouldDismissCard = abs(gesture.translation(in: nil).x) > threshold

    if shouldDismissCard {
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.1, options: .curveEaseOut, animations: {
            self.frame = CGRect(x: 1000 * translationDirection, y: 0, width: self.frame.width, height: self.frame.height)
        }) { (_) in
            self.removeFromSuperview()
        }
    } else {
        UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.1, options: .curveEaseOut, animations: {
            self.transform = .identity // Back to origin
        }) { (_) in
            print("Snapped back")
        }
    }

当我查看控制台时,.began在.end状态下点击并在此状态下制作动画时尝试中断时,.began不会启动。

如何重置动画或在此处停止动画,我不知道在处于.end状态的动画仍在运行时如何触发.began。

0 个答案:

没有答案
相关问题