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

Ios 在Swift中获取多个触摸的坐标,ios,swift,coordinates,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) } 但是当用两个手指触摸

所以我一直在胡闹,试图得到屏幕上触摸的坐标。到目前为止,我可以用这个获得一次触摸的坐标:

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

但是当用两个手指触摸时,我只得到第一次触摸的坐标。多点触摸工作(我用这个小教程测试:)。所以我的问题是,如何获得第二次(第三次,第四次…)触摸的坐标

您必须迭代不同的触摸。这样,您就可以访问每一次触摸

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

给定的
触摸
参数是一组检测到的触摸。 您只能看到一个触摸,因为您选择了以下触摸之一:

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)
}

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

首先,记住通过设置启用多点触摸事件

self.view.isMultipleTouchEnabled = true
UIViewController
的代码中,或在Xcode中使用适当的情节提要选项:

否则,在
touchesbeated
()中,您将始终获得单触

然后,在
触摸开始时
,在触摸集合上迭代以获取其坐标:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self.view)
        print(location)
    }
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
接触{
让位置=touch.location(在:self.view中)
打印(位置)
}
}

在Swift 1.2中,这一点已经发生了变化,
ToucheSBegind
现在提供了一组NSObject。 要遍历这些对象,请将Touchs集合强制转换为一组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)
    }
}
override func touchsbegined(touchs:Set,withEvent-event:UIEvent){
var touchet=toucheas!Set
用于触摸屏中的触摸{
让位置=touch.locationInView(self.view)
println(位置)
}
}

根据@Andrew的回答,Swift 3的

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)
    }
}
override func touchesMoved(touch:Set,带有事件:UIEvent?){
让触摸设置=触摸
用于触摸屏中的触摸{
让位置=touch.location(在:self.view中)
打印(位置)
}
}
编辑,我的错,这没有回答你的问题,有同样的问题,有人将我链接到:

无论如何,我必须改变一些东西才能使其在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
            }
        }
    }
}
var fingers=[String?](重复:nil,计数:5)
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
super.touchesbeated(touches,with:event)
接触{
让点=触摸位置(在:self.view中)
用于手指中的(食指、手指)。枚举(){
如果finger==nil{
手指[索引]=字符串(格式:“%p”,触摸)
打印(“手指\(索引+1):x=\(点x),y=\(点y)”)
打破
}
}
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
super.touchesMoved(touches,with:event)
接触{
让点=触摸位置(在:self.view中)
用于手指中的(食指、手指)。枚举(){
如果let finger=finger,finger==字符串(格式:“%p”,触摸){
打印(“手指\(索引+1):x=\(点x),y=\(点y)”)
打破
}
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
super.touchesend(触摸,带有:事件)
接触{
用于手指中的(食指、手指)。枚举(){
如果let finger=finger,finger==字符串(格式:“%p”,触摸){
手指[食指]=零
打破
}
}
}
}
我仍然有一个小问题,但我认为它与我的代码中的手势识别器有关。 但这应该能奏效。
它将打印控制台中每个点的坐标

在Swift 3,4中 通过其散列来识别触摸指针:

// SmallDraw
func pointerHashFromTouch(_ touch:UITouch) -> Int {
    return Unmanaged.passUnretained(touch).toOpaque().hashValue
} 

谢谢你的回答!我认为乔拉什的回答最有帮助,因为他提供了更多的信息,但两种方法都有效:)不客气!当然,这是你的决定:)我对答案做了一点编辑,并在文档中留下了参考,强调了启用多点触摸功能的重要性,否则代码将无法工作(我们总是忘记这一点!)。我留下这个答案作为其他可能需要这个细节的人的参考:)我应该把这个答案标记为这个问题的答案吗?(因为现在标记的答案不再是一个真正的答案)如果有人能确认这是有效的,我会将其标记为答案