获取Swift中多个触摸的坐标

时间:2015-02-24 12:59:54

标签: ios swift coordinates

所以我一直在试图获取屏幕上的触摸坐标。到目前为止,我可以通过以下方式获得一次触摸的坐标:

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)。所以我的问题是,如何获得第二次(和第三次,第四次......)触摸的坐标?

5 个答案:

答案 0 :(得分:15)

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

首先,请记住通过设置

启用多点触控事件
self.view.isMultipleTouchEnabled = true

在您的UIViewController代码中,或使用Xcode中相应的故事板选项:

xcode screenshot

否则,您将始终只需touchesBegansee documentation here)即可轻触。

然后,在touchesBegan内,迭代一组触摸以获得它们的坐标:

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

答案 1 :(得分: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)
}

答案 2 :(得分:1)

你必须迭代不同的触摸。这样你就可以获得每次触摸。

for touch in touches{
  //Handle touch
  let touchLocation = touch.locationInView(self.view)
}

答案 3 :(得分: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)
    }
}

答案 4 :(得分: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)
    }
}

编辑,我的不好,没有回答你的问题,遇到了同样的问题,有人把我联系到this previous answer

无论如何,我必须改变一些东西才能使它在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相关联。 但那应该可以解决问题。 它会打印出你的consol中每个点的坐标。