同时解析 UITapGestureRecognizer 和 UILongPressGestureRecognizer 并在手指按下时触发 UIGestureRecognizer.State.began

Resolve UITapGestureRecognizer and UILongPressGestureRecognizer simultaneously and fire UIGestureRecognizer.State.began on finger touch down

首先,none 个已经回答的问题对我没有帮助

  1. Long Press Gesture Recognizer Only Fired When Finger is Lifted
  2. 以此类推

除了一件事,代码几乎可以正常工作:只有当我从屏幕上抬起手指时才会调用长按手势。但我需要获得像 Instagram 故事中那样的行为(当您可以在故事之间切换并按住手指暂停某些故事时)。

我的问题更多是关于如何在用户手指向下而不是向上触摸时强制触发 UILongPressGesture。

这是我的代码:

private func setupTapGestures() {
    tapRecognizer = UITapGestureRecognizer()
    tapRecognizer?.addTarget(self, action: #selector(handleTapGesture(_:)))
    tapRecognizer?.delegate = self
    view.addGestureRecognizer(tapRecognizer!)

    longPressRecognizer = UILongPressGestureRecognizer()
    longPressRecognizer?.addTarget(self, action: #selector(handleLongPressGesture(_:)))
    longPressRecognizer?.minimumPressDuration = 0.1
    longPressRecognizer?.delegate = self
    view.addGestureRecognizer(longPressRecognizer!)
}

@objc func handleTapGesture(_ gestureRecognizer: UIGestureRecognizer) {
    let width = view.frame.width
    let point = gestureRecognizer.location(in: view)
    viewModel?.tapAction(viewWidth: width, tapPoint: point)
    Swift.print("Tap gesture")
}

@objc func handleLongPressGesture(_ gestureRecognizer: UILongPressGestureRecognizer) {
    if gestureRecognizer.state == .began {
        Swift.print("Began")
    } else if gestureRecognizer.state == .ended {
        Swift.print("Ended")
    }
}

UIGestureRecognizerDelegate:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    // Don't recognize a single tap until a long-press fails
    if gestureRecognizer == tapRecognizer && otherGestureRecognizer == longPressRecognizer {
        return true
    }
    return false
}

shouldRequireFailureOf docs

有什么建议或想法吗?

我想知道您对 shouldRequireFailureOf 的实施是否导致了问题?

这对我来说效果很好(注意:我使用 .minimumPressDuration = 0.25 因为在 0.1 秒内点击有点困难):

class GestureTestViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupTapGestures()
    }

    private func setupTapGestures() -> Void {

        let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
        view.addGestureRecognizer(singleTapGesture)

        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGesture(_:)))
        longPressGesture.minimumPressDuration = 0.25
        view.addGestureRecognizer(longPressGesture)

    }

    @objc func handleLongPressGesture(_ gesture: UILongPressGestureRecognizer) -> Void {
        if gesture.state == .began {
            print("Did Long Press (began)")
        }
        if gesture.state == .ended {
            print("Did Long Press (ended)")
        }
    }

    @objc func handleTapGesture(_ gesture: UITapGestureRecognizer) -> Void {
        print("Did Single Tap")
    }

}

当我 点击 时,我在调试控制台中得到 "Did Single Tap"

当我点击并按住时,我很快得到"Did Long Press (began)",而在手指抬起时我得到"Did Long Press (ended)"