2015-02-24 10 views
6

Quindi ho cercato di ottenere le coordinate dei tocchi sullo schermo. Finora posso ottenere le coordinate di un contatto con questo:Ottieni le coordinate di più tocchi in Swift

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

Ma quando si tocca con due dita Ho solo ottenere le coordinate del primo tocco. Multi-touch funziona (ho provato con questo piccolo tutorial: http://www.techotopia.com/index.php/An_Example_Swift_iOS_8_Touch,_Multitouch_and_Tap_Application). Quindi la mia domanda è: come ottengo le coordinate del secondo (e terzo, quarto ...) tocco?

risposta

4

la proposta touches argomento è un insieme di tocchi identificati. Si vede solo tocco, perché si seleziona uno degli accorgimenti con:

touches.anyObject() // Selects a random object (touch) from the set 

Al fine di ottenere tutti i tocchi di iterare il dato insieme

for obj in touches.allObjects { 
    let touch = obj as UITouch 
    let location = touch.locationInView(self.view) 
    println(location) 
} 
1

È necessario ripetere i diversi tocchi. In questo modo puoi accedere ad ogni tocco.

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

** Aggiornamento a Swift 4 e Xcode 9 (8 Ottobre 2017) **

Prima di tutto, ricordatevi di attivare gli eventi multi-touch impostando

self.view.isMultipleTouchEnabled = true 

in S' di codice, o utilizzando l'opzione storyboard appropriata in Xcode tuoi UIViewController:

xcode screenshot

Altrimenti otterrai sempre un solo tocco a touchesBegan (see documentation here).

Poi, all'interno touchesBegan, iterare l'insieme di tocchi per ottenere le loro coordinate:

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

Grazie per la risposta! Ho segnato la risposta di giorashc come più utile perché mi ha dato qualche informazione in più, ma entrambi i metodi funzionano :) – Fr4nc3sc0NL

+0

Prego! Ovviamente è la tua chiamata :) Ho modificato la risposta un po 'e ho lasciato riferimenti alla documentazione, sottolineando come sia importante abilitare il multi-touch o il codice non funzionerà (ci dimentichiamo sempre!). Sto lasciando questa risposta come riferimento per le altre persone che potrebbero aver bisogno di quel dettaglio :) – Para

1

In Swift 1.2 questo è cambiato, e touchesBegan ora fornisce una serie di NSObjects. Per scorrere attraverso di loro, scagli la raccolta tocchi come oggetti di un insieme di UITouch come segue:

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

Devo contrassegnare questa risposta come risposta alla domanda? (Poiché la risposta ora contrassegnata non è più una risposta) – Fr4nc3sc0NL

0

Per Swift 3, sulla base di @ risposta di 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) 
    } 
} 

EDIT, il mio male, che non è rispondere alla tua domanda, ha avuto lo stesso problema e qualcuno mi ha legato a this previous answer:

Comunque, ho dovuto cambiare alcune cose per rendere funziona in rapida 3, ecco il mio codice corrente:

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

Ho ancora un piccolo problema ma penso che sia collegato al mio GestureRecognizer nel mio codice. Ma quello dovrebbe fare il trucco. Ti stamperà le coordinate di ogni punto nella tua console.

+0

Se qualcuno può confermare che questo funziona, contrassegnerò questa come risposta – Fr4nc3sc0NL

+0

@ Fr4nc3sc0NL Modificata la mia risposta per correggerla. – Hawkydoky

0

In Swift 3,4 Identificare puntatore tocco dal suo hash:

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