Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 平移手势识别器:一旦松开平移手势,触摸位置就会崩溃_Ios_Swift_Uipangesturerecognizer - Fatal编程技术网

Ios 平移手势识别器:一旦松开平移手势,触摸位置就会崩溃

Ios 平移手势识别器:一旦松开平移手势,触摸位置就会崩溃,ios,swift,uipangesturerecognizer,Ios,Swift,Uipangesturerecognizer,我正在使用pan手势识别器,我需要检测初始接触点以便采取行动。我正在使用以下代码 @IBAction func panDetected(sender: UIPanGestureRecognizer) { let touchPoint = sender.locationOfTouch(0, inView: nil) println(touchPoint.x) } 当我移动手指时,这些功能会打印触摸点。然而,一旦我将手指从屏幕上提起,它就会崩溃。这里是崩溃消息 Terminatin

我正在使用pan手势识别器,我需要检测初始接触点以便采取行动。我正在使用以下代码

@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).'
正如这篇文章所建议的,我已经尝试过设置最小和最大触摸限制;


有什么问题吗

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

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

let touchPoint = sender.locationInView(sender.view)