在Swift中获取多个触摸点的坐标

7

我一直在尝试获取屏幕上触摸点的坐标,目前我可以用以下代码获取一个触摸点的坐标:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.anyObject()! as UITouch
    let location = touch.locationInView(self.view)
    println(location)
}

但是当用两个手指触摸时,我只能得到第一个触摸的坐标。多点触控是可以实现的(我用这个小教程测试过:http://www.techotopia.com/index.php/An_Example_Swift_iOS_8_Touch,_Multitouch_and_Tap_Application)。所以我的问题是,如何获取第二个(第三个,第四个...)触摸的坐标?

6个回答

15

**更新至Swift 4和Xcode 9(2017年10月8日)**

首先,通过设置启用多点触控事件

self.view.isMultipleTouchEnabled = true

你可以在你的UIViewController代码中,或者使用Xcode中适当的storyboard选项:

xcode screenshot

否则,你总是会在touchesBegan方法中只得到一个单独的触摸对象(参见此处文档)。

然后,在touchesBegan方法内部,遍历所有触摸对象以获取它们的坐标:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self.view)
        print(location)
    }
}

谢谢你的回答!我将giorashc的答案标记为最有帮助的,因为他提供了稍微更多的信息,但两种方法都可以 :) - Fr4nc3sc0NL
不客气!当然由你决定 :) 我稍微编辑了答案,并在文档中留下了参考,强调启用多点触控的重要性,否则代码将无法正常工作(我们总是忘记这一点!)。我将此答案留作其他可能需要该细节的人的参考 :) - Para

4
给定的touches参数是一组检测到的触摸。您只能看到一个触摸,因为您使用以下方式选择其中一个触摸:
touches.anyObject() // Selects a random object (touch) from the set

为了获取所有触摸点,请迭代给定的集合。
for obj in touches.allObjects {
   let touch = obj as UITouch
   let location = touch.locationInView(self.view)
   println(location)
}

1
在Swift 1.2中有所改变,touchesBegan现在提供了一个NSObject的集合。 要遍历它们,将touches集合强制转换为UITouch对象的集合,代码如下所示:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    var touchSet = touches as! Set<UITouch>

    for touch in touchSet{
        let location = touch.locationInView(self.view)
        println(location)
    }
}

我应该将这个答案标记为问题的答案吗?(因为现在标记的答案不再是一个真正的答案) - Fr4nc3sc0NL

1
你需要遍历不同的触摸点。这样你就可以访问每一个触摸点。
for touch in touches{
  //Handle touch
  let touchLocation = touch.locationInView(self.view)
}

0
在Swift 3,4中,通过其哈希值来识别触摸指针:
// SmallDraw
func pointerHashFromTouch(_ touch:UITouch) -> Int {
    return Unmanaged.passUnretained(touch).toOpaque().hashValue
} 

0

对于Swift 3,基于@Andrew的答案:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touchSet = touches

    for touch in touchSet{
        let location = touch.location(in: self.view)
        print(location)
    }
}

编辑,我的错,那并没有回答你的问题,我也遇到了同样的问题,有人给我提供了这个之前的答案

无论如何,我不得不改变一些东西才能使它在Swift 3中工作,这是我的当前代码:

var fingers = [String?](repeating: nil, count:5)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    for touch in touches{
        let point = touch.location(in: self.view)
        for (index,finger)  in fingers.enumerated() {
            if finger == nil {
                fingers[index] = String(format: "%p", touch)
                print("finger \(index+1): x=\(point.x) , y=\(point.y)")
                break
            }
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    for touch in touches {
        let point = touch.location(in: self.view)
        for (index,finger) in fingers.enumerated() {
            if let finger = finger, finger == String(format: "%p", touch) {
                print("finger \(index+1): x=\(point.x) , y=\(point.y)")
                break
            }
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    for touch in touches {
        for (index,finger) in fingers.enumerated() {
            if let finger = finger, finger == String(format: "%p", touch) {
                fingers[index] = nil
                break
            }
        }
    }
}

我还有一个小问题,但我认为它与我的代码中的GestureRecognizer相关联。 但那应该会起作用。 它将在控制台中为您打印每个点的坐标。


如果有人能确认这个代码可以正常工作,我会将其标记为答案。 - Fr4nc3sc0NL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接