简体   繁体   English

如何触发多个UIGestureRecognizers而无需从屏幕上抬起手指

[英]How to trigger multiple UIGestureRecognizers without lifting finger from screen

I have adjacent views on the screen, and each have a different UIGestureRecognizer . 我在屏幕上有相邻的视图,每个视图都有不同的UIGestureRecognizer What I want is if I touch down in one view and move into an adjacent view, the adjacent view's gesture recognizer will start detecting the touches. 我想要的是如果我在一个视图中向下触摸并移动到相邻视图中,相邻视图的手势识别器将开始检测触摸。

What is currently happening is if I touch down in one view and move out of that view's frame, the initial view's gesture recognizer will continue to receive calls to touchChanged until my finger has lifted. 目前正在发生的事情是,如果我在一个视图中向下触摸并移出该视图的帧,初始视图的手势识别器将继续接收对touchChanged调用,直到我的手指抬起。 And it doesn't matter if I change the state of the gesture recognizer to .ended , .failed , or .cancelled , no other gesture recognizers will receive calls to their touch methods until my finger is lifted. 如果我将手势识别器的state更改为.ended.failed.cancelled并不.cancelled ,在我的手指被抬起之前,其他手势识别器将不会接收其touch方法的调用。

How can I ensure a gesture recognizer will be triggered when the touch is in its view's bounds, and end when it leaves them, without having to handle touch events from the superview? 当触摸处于视图边界时,如何确保触发手势识别器,并且当触摸离开时,如何在不必处理来自superview的触摸事件的情况下结束?

Here is the custom UIGestureRecognizer I implemented to do this: 这是我实现的自定义UIGestureRecognizer

class GestureRecognizer: UIGestureRecognizer, UIGestureRecognizerDelegate {
    override init(target: Any?, action: Selector?) {
        super.init(target: target, action: action)
        delegate = self
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesBegan(touches, with: event)
        state = .began
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesMoved(touches, with: event)
        if state == .began {state = .ended}
        else {state = .began}
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return otherGestureRecognizer is GestureRecognizer
    }
}

You can do this, Just write this delegate method in your controller, and return true. 你可以这样做,只需在你的控制器中编写这个委托方法,然后返回true。

read this once. 读一次这个。 https://developer.apple.com/reference/uikit/uigesturerecognizerdelegate/1624208-gesturerecognizer https://developer.apple.com/reference/uikit/uigesturerecognizerdelegate/1624208-gesturerecognizer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不将手指从屏幕抬起的情况下结束长按手势? - How to end long press gesture without lifting finger from screen? Flutter - 无需抬起手指即可进行多种手势 - Flutter - Multiple gestures without lifting the finger iOS:检测touchEnded是否来自滑动屏幕或抬起手指? - iOS: Detect if touchEnded comes from sliding off screen or lifting finger? 如何触摸UIImageView,然后替换为另一个UIImageView,当手指移动时拖动该UIImageView,而无需抬起手指 - how to touch UIImageView, then replace with another UIImageView, which drags when finger moves, without lifting finger 当手指在屏幕上而不抬起时,jos动画在ios safari webkit浏览器中停止 - javascript animation halted in ios safari webkit browser when finger is on the screen without lifting 使用PanGestureRecognizer创建UIView并在不抬起手指的情况下激活它 - Creating a UIView with PanGestureRecognizer and activating it without lifting the finger 如何在不产生触摸事件的情况下将手指从触摸屏上抬起? - How to lift a finger from a touch screen without generating touch events? 举手指时如何检测意外触摸? - How to detect unintentional touches when lifting a finger? 如何检测多个 UIGestureRecognizers 结束? - How to detect that multiple UIGestureRecognizers end? 触摸按钮,无需从屏幕上移开手指 - Touch button without removing finger from screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM