ScrollView平移手势和panGestureRecognizer

时间:2018-01-28 21:48:13

标签: ios swift uiscrollview uipangesturerecognizer

我在UIScrollView中有一个UIViewController,它由一个segue以模态显示,另一个UIPanGestureRecognizer通过pan取消视图控制器。此手势仅在

时有效
 scrollView.contentOffset.y == 0

问题是,现在两个平移手势相互冲突,我不能再滚动视图了。

为了解决这个问题,我尝试使用gestureRecognizer(_: shouldRecognizeSimultaneouslyWith:)方法,返回是,而且,我尝试将自定义平移手势添加到UIScrollView平移手势识别器,如下所示:

 scrollView.panGestureRecognizer.addTarget(self, action: #selector(handlePanGesture(_:)))

但这些并没有解决问题 如果您知道如何解决这个问题,我将非常感谢您的帮助。

EDITED

以下是解除视图控制器的平移手势的代码:

     @IBAction func handlePanGesture(_ sender: UIPanGestureRecognizer) {
    let percentThreshold: CGFloat = 0.3

    if scrollView.contentOffset.y == 0 {
        let translation = sender.translation(in: view)
        let verticalMovement = translation.y / view.bounds.height
        let downwardMovement = fmaxf(Float(verticalMovement), 0.0)
        let downwardMovementPercent = fminf(downwardMovement, 1.0)
        let progress = CGFloat(downwardMovementPercent)

        guard let interactor = interactor else {return}
        switch sender.state {
        case .began:
            interactor.hasStarted = true
            dismiss(animated: true, completion: nil)
        case .changed:
            interactor.shouldFinish = progress > percentThreshold
            interactor.update(progress)
        case .cancelled:
            interactor.hasStarted = false
            interactor.cancel()
        case .ended:
            interactor.hasStarted = false
            interactor.shouldFinish ? interactor.finish() : interactor.cancel()
        default:
            break
        }

    }
}

EDITED_2 以下是Interactor的代码:

class Interactor: UIPercentDrivenInteractiveTransition {
var hasStarted = false
var shouldFinish = false 

}

P.S。我知道有很多类似的问题,但它们对我不起作用。

4 个答案:

答案 0 :(得分:2)

我不确定,但您可以尝试添加ViewController作为滑动的UIPanGestureRecognizer代表来解除平移手势并实施gestureRecognizerShouldBegin(_:);

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return scrollView.contentOffset.y == 0
    }

因此,只有在内容偏移量为零时才会启动要关闭的手势。

答案 1 :(得分:2)

要在ScrollView上显示UIPanGestureRecognizer时允许滚动,您需要创建UIGestureRecognizerDelegate,在gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)上返回true

如果不这样做,则无法在ScrollView上滚动。

这样做是这样的:

let scrollViewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(onPan(_:)))
scrollViewPanGesture.delegate = self
scrollView.addGestureRecognizer(scrollViewPanGesture)

extension ViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

答案 2 :(得分:0)

在滚动视图下添加子视图并向其添加平移手势,而不是将其添加到self.view,这肯定会与scrollview的一个

冲突

答案 3 :(得分:0)

在实施gestureRecognizer(_: shouldRecognizeSimultaneouslyWith:)时,您采取了正确的方式 但您必须先将手势委托设置为当前的View Controller:

let panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(handlePanGesture(_:)))
panGesture.delegate = self // <--THIS
scrollView.addGestureRecognizer(panGesture)