我的问题:有没有办法调整 UIPanGestureRecognizer 的“敏感度”,使其“更快”开启,即在移动较少数量的“像素”之后?
我有一个带有 UIImageView 的简单应用程序,以及与此相关联的捏合和平移手势识别器,以便用户可以放大图像并手动在图像上绘制。工作正常。
但是,我注意到常用的 UIPanGestureRecognizer 不会返回 UIGestureRecognizerState.Changed 的值,直到用户的手势移动了大约 10 个像素。
示例:这是一张屏幕截图,显示了我尝试绘制的几条线越来越短,并且有一个明显的有限长度,低于该长度不会绘制任何线,因为平移手势识别器永远不会改变状态。
IllustrationOfProgressivelyShorterLines.png
...即,在黄线的右侧,我仍在尝试绘制,并且我的触摸被识别为 touchesMoved 事件,但是 UIPanGestureRecognizer 没有触发它自己的“移动”事件,因此什么都没有被吸引。
(注意/澄清:该图像占据了我 iPad 的整个屏幕,因此即使在识别器没有发生状态变化的情况下,我的手指物理移动也超过一英寸。只是我们'根据捏合手势识别器生成的转换重新“放大”,因此图像的几个“像素”占据了大量屏幕。)
这不是我想要的。关于如何解决它的任何想法?
如果我对它进行子分类,也许我可以获得 UIPanGestureRecognizer 的一些“内部”参数?我想我会尝试以诸如...的方式对识别器进行子类化。
class BetterPanGestureRecognizer: UIPanGestureRecognizer {
var initialTouchLocation: CGPoint!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
initialTouchLocation = touches.first!.locationInView(view)
print("pan: touch begin detected")
print(self.state.hashValue) // this lets me check the state
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
print("pan: touch move detected")
print(self.state.hashValue) // this remains at the "began" value until you get beyond about 10 pixels
let some_criterion = (touches.first!.isEqual(something) && event.isEqual(somethingElse))
if (some_criterion) {
self.state = UIGestureRecognizerState.Changed
}
}
}
...但我不确定要为 some_criterion 等使用什么
有什么建议吗?
.
其他可行的替代方案,但我宁愿不必这样做:
谢谢。
最佳答案
[如果值得的话,我会选择你的答案而不是我的答案(即以下),所以我暂时不会“接受”这个答案。]
明白了。该解决方案的基本思想是在触摸移动时更改状态,但使用关于同步手势识别器的委托(delegate)方法,以免“锁定”任何捏合(或旋转)手势。这将允许单指和/或多指平移,如您所愿,没有“冲突”。
那么,这是我的代码:
class BetterPanGestureRecognizer: UIPanGestureRecognizer, UIGestureRecognizerDelegate {
var initialTouchLocation: CGPoint!
override init(target: AnyObject?, action: Selector) {
super.init(target: target, action: action)
self.delegate = self
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
initialTouchLocation = touches.first!.locationInView(view)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
if UIGestureRecognizerState.Possible == self.state {
self.state = UIGestureRecognizerState.Changed
}
}
func gestureRecognizer(_: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool {
if !(shouldRecognizeSimultaneouslyWithGestureRecognizer is UIPanGestureRecognizer) {
return true
} else {
return false
}
}
}
通常将“shouldRecognizeSimultaneouslyWithGestureRecognizer”委托(delegate)设置为 true always 是许多人可能想要的。如果另一个识别器是另一个 Pan,我让委托(delegate)返回 false,只是因为我注意到没有那个逻辑(即,无论如何让委托(delegate)返回 true),它是“通过” Pan 手势到底层 View ,我不想那样。无论如何,您可能只想让它返回 true。干杯。
关于iOS UIPanGestureRecognizer : adjust sensitivity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32130537/