【问题标题】:Resolve UITapGestureRecognizer and UILongPressGestureRecognizer simultaneously and fire UIGestureRecognizer.State.began on finger touch down同时解决 UITapGestureRecognizer 和 UILongPressGestureRecognizer 并在手指触摸时触发 UIGestureRecognizer.State.began
【发布时间】:2019-12-21 23:47:18
【问题描述】:

首先,已经回答的问题都没有帮助我

  1. Swift: Long Press Gesture Recognizer - Detect taps and Long Press
  2. Using tap gesture and long press at the same time in Table View
  3. Long Press Gesture Recognizer Only Fired When Finger is Lifted
  4. 等等

除了一件事之外,代码几乎可以正常工作:长按手势仅在我将手指从屏幕上抬起时才会调用。但我需要像在 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

有什么建议或想法吗?

【问题讨论】:

    标签: ios swift uigesturerecognizer uitapgesturerecognizer uilongpressgesturerecogni


    【解决方案1】:

    我想知道您对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 (begin)”,当我抬起手指时,我会得到“Did Long Press (结束)”

    【讨论】:

    • 嗯,你的代码在操场上工作,但在我的项目中不行。会调查的,谢谢!
    • 好吧,对于任何可能遇到相同意外行为的人,我一直在使用自定义过渡 UIViewControllerAnimatedTransitioning,所以当我禁用它时,一切正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多