我有一个自定义的UIGestureRecognizer用于双指手势,它完美地工作,只是很挑剔同时触摸iOS设备以使touchesBegan
调用2个触摸。即使我尝试使用两个手指,touchesBegan
经常只被一个Touch调用。
有没有办法使对触摸数量的识别更具容忍性,以便您可以更灵活地在触摸屏幕上放置手指?
我注意到,即使您首先放下一个手指,然后再过一段时间放下另一个手指,但仍然保持第一个手指按下,双指轻敲也会被识别。
这是我的touchesBegan
函数的代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
if touches.count != 2 {
state = .failed
return
}
// Capture the first touch and store some information about it.
if trackedTouch == nil {
trackedTouch = touches.min { $0.location(in: self.view?.window).x < $1.location(in: self.view?.window).x }
strokePhase = .topSwipeStarted
topSwipeStartPoint = (trackedTouch?.location(in: view?.window))!
// Ignore the other touch that had a larger x-value
for touch in touches {
if touch != trackedTouch {
ignore(touch, for: event)
}
}
}
}
if touches.count != 2 { state = .failed }
。这与你说的完全相反。你说你不希望仅仅因为计数不为2就立刻失败,所以不要这样做! - matt