【问题标题】:Pan Gesture Recognizer: Location of touch crashes once the pan gesture is released平移手势识别器:一旦释放平移手势,触摸位置就会崩溃
【发布时间】:2014-11-22 20:12:20
【问题描述】:

我正在使用平移手势识别器,我需要检测初始触摸点才能采取行动。我正在使用以下代码;

@IBAction func panDetected(sender: UIPanGestureRecognizer) {
    let touchPoint = sender.locationOfTouch(0, inView: nil)
    println(touchPoint.x)
}

当我移动手指时,这些功能会打印触摸点。但是,一旦我从屏幕上抬起手指,它就会崩溃。这是崩溃消息;

Terminating app due to uncaught exception 'NSRangeException', reason: '-[UIPanGestureRecognizer locationOfTouch:inView:]: index (0) beyond bounds (0).'

我已经按照这篇文章的建议尝试设置最小和最大触摸限制; PAN Gesture is crashing while taking the location of touch in view in iOS

可能是什么问题?

【问题讨论】:

    标签: ios swift uipangesturerecognizer


    【解决方案1】:

    您遇到的问题是,一旦手势结束,它就不再有任何触摸,因此在索引 0 处不再有触摸可检索。您可能只想使用locationInView 方法:

    let touchPoint = sender.locationInView(sender.view)
    

    (这对视图使用sender.view 而不是nil,因为在手势识别器添加到的视图的坐标空间中获取触摸位置通常比在窗口的坐标空间中更有用)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多