iOS UIPanGestureRecognizer:调整灵敏度?

时间:2015-08-21 00:24:29

标签: ios swift uipangesturerecognizer

我的问题:有没有办法调整"灵敏度" UIPanGestureRecognizer,以便它可以更快地打开,即在移动更少数量的像素之后?

我有一个带有UIImageView的简单应用程序,以及与此相关的捏合和平移手势识别器,以便用户可以放大并手动绘制图像。工作正常。

但是,我注意到UIPanGestureRecognizer的股票没有返回UIGestureRecognizerState.Changed的值,直到用户的手势移动了大约10个像素。

示例:这里有一个屏幕截图,显示了我尝试绘制更短和更短的几行。更短,并且有一个明显的有限长度,在此之下没有画线,因为平移手势识别器永远不会改变状态。

IllustrationOfProgressivelyShorterLines.png

...也就是说,在黄线的右侧,我仍在尝试画画,我的触摸被识别为touchesMoved事件,但UIPanGestureRecognizer并没有触发自己的" Moved&# 34;因此没有任何事情被吸引。

(注意/澄清:该图像占据了我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等。
有什么建议吗?

其他可行的替代方案,但我不必这样做:

  1. 可以简单地将我的UIPanGestureRecognizer附加到某个父级, 非缩放视图,然后使用仿射变换&amp;这样重新映射 平移点接触图像的各个部分。 那我为什么不这样做呢?因为代码是这样编写的 许多其他对象挂起图像视图,他们都得到了 相同的手势识别器和....一切都很好,没有 我跟踪任何事情(例如仿射变换),如果你真的真的放大了,问题就会出现。
  2. 我可以放弃UIPanGestureRecognizer,并有效地使用touchesBegan和touchesMoved编写我自己的(这是一种 我在做什么,但我喜欢UIPanGestureRecognizer 以我不喜欢的方式区别于捏事件 我不得不担心自己编码。
  3. 我可以指定一些用户无法超越的最大缩放比例。这无法实现我的目标,即我希望允许精细细节操作。
  4. 感谢。

1 个答案:

答案 0 :(得分:1)

[如果有的话会选择你的答案(例如,以下),所以我不会'接受'这个答案。]

知道了。解决方案的基本思想是每当移动触摸时改变状态,但是使用关于同时手势识别器的委托方法,以便不“锁定”任何捏(或旋转)手势。这样就可以根据需要进行单指和/或多指平移,没有“冲突”。

然后,这是我的代码:

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”委托给true 总是是很多人可能想要的。如果另一个识别器是另一个Pan,我让委托返回false,只是因为我注意到没有那个逻辑(即,无论如何让委托返回true),它是“通过”Pan手势到底层视图而我不想要的。无论如何,你可能只想让它返回真实。欢呼声。

相关问题