[英]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.