Swift

2015-02-24 31 views
6

Birden çok dokunuşun koordinatlarını alın. Ekrandaki dokunuşların koordinatlarını almaya çalışıyorum.Swift

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

Ama iki parmakla dokunulduğunda ben sadece ilk dokunuş koordinatlarını almak: Şimdiye kadar bu konuda tek dokunuşla koordinatlarını alabilirsiniz. Multi-touch çalışmaları (Bu küçük eğitici ile test ettim: http://www.techotopia.com/index.php/An_Example_Swift_iOS_8_Touch,_Multitouch_and_Tap_Application). Benim sorum şu ki, ikinci (ve üçüncü, dördüncü) dokunuşun koordinatlarını nasıl alabilirim?

cevap

4

verilen touches bağımsız değişken tespit dokunur kümesidir. Bütün dokunuşlar verilen Swift 4 ve Xcode 9'a Güncelleme

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

Farklı dokunuşlar üzerinde yinelemek zorundasınız. Bu şekilde her dokunuşa erişebilirsiniz.

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

** ayarlamak yineleme alabilmek için (8

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

: Birlikte dokunuşlar birini seçin çünkü Yalnızca bir dokunuş bakın Ekim 2017) ** her

Öncelikle

self.view.isMultipleTouchEnabled = true 
ayarlayarak
çoklu dokunma olaylarını etkinleştirmeyi unutmayın içinde

sizin UIViewController 'ın kodunu veya Xcode uygun film şeridi seçeneğini kullanarak:

xcode screenshot

Aksi takdirde her zaman touchesBegan ( see documentation here) tek dokunuş elde edersiniz. Swift 1.2 olarak

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

Cevabınız için teşekkür ederiz! Giorashc'ın cevabını en yararlı olarak işaretledim çünkü biraz daha fazla bilgi verdi, ama her iki yöntem de işe yarar :) – Fr4nc3sc0NL

+0

Bir şey değil! Bu sizin aramanız, tabii ki :) Cevabı biraz düzenledim ve belgelere referanslar bıraktım, çoklu dokunmayı etkinleştirmenin ya da kodun çalışmaz hale gelmesinin altını çiziyor (bunu her zaman unutuyoruz!). Bu cevabı bu ayrıntıya ihtiyaç duyabilecek diğer insanlar için referans olarak bırakıyorum :) – Para

1

bu değişti ve touchesBegan şimdi NSObjects bir Set sunar:

Sonra touchesBegan içinde koordinatlarını almak için dokunur kümesi üzerinden yineleme.

Andrew cevap @ dayalı Swift 3 için
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

Bu cevabı soruya cevap olarak mı işaretlemeliyim? (Artık işaretlenmiş cevap gerçekten bir cevap olmadığından) – Fr4nc3sc0NL

0

,:

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'e, Benim hatam, o değil UITouch bir Set nesneleri olarak aşağıdaki gibi içlerinden yineleme için dokunuşlar koleksiyonu dökme sorunuzu yanıtlamadan, aynı problem vardı ve birisi this previous answer beni bağlantılı: Ben hızlı 3'te çalıştığından emin olmak için birkaç şey değiştirmek zorunda Neyse

, burada benim geçerli kod:

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

Hala biraz sorunum var ama bence kodumdaki GestureRecognizer ile bağlantılı. Ama bu hile yapmalı. Size, konsolunuzdaki her bir noktanın koordinatını yazdıracaktır.

+0

Birisi bunun çalıştığını onaylayabilirse, bunu cevap olarak işaretleyeceğim – Fr4nc3sc0NL

+0

@ Fr4nc3sc0NL Cevabımı düzeltmek için düzenledim. – Hawkydoky

0
Swift 3,4 yılında

karması ile dokunmatik işaretçi tanımlayın:

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