2015-02-24 7 views
6

में कई स्पर्शों के निर्देशांक प्राप्त करें, इसलिए मैं स्क्रीन पर छूने के निर्देशांक प्राप्त करने की कोशिश कर रहा हूं। अब तक मैं इस के साथ एक स्पर्श के निर्देशांकों को प्राप्त कर सकते हैं:स्विफ्ट

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)। तो मेरा सवाल यह है कि, मैं दूसरे (और तीसरे, चौथे ...) स्पर्श के निर्देशांक कैसे प्राप्त करूं?

उत्तर

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

आपको अलग-अलग स्पर्शों पर फिर से चलना होगा। इस तरह आप हर स्पर्श तक पहुंच सकते हैं।

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

** स्विफ्ट 4 और Xcode 9 पर अपडेट किया गया प्राप्त करने के लिए (8 अक्टू 2017) **

सबसे पहले,

self.view.isMultipleTouchEnabled = true 
सेट करके
मल्टी टच घटनाओं सक्षम करने के लिए याद

में अपने UIViewController के कोड, या Xcode में उपयुक्त स्टोरीबोर्ड विकल्प का उपयोग:

xcode screenshot

नहीं तो आप हमेशा touchesBegan (see 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) 
    } 
} 
+0

अपने जवाब के लिए धन्यवाद! मैंने जियोराशक के उत्तर को सबसे उपयोगी बताया क्योंकि उसने थोड़ा और जानकारी दी, लेकिन दोनों विधियां काम करती हैं :) – Fr4nc3sc0NL

+0

आपका स्वागत है! यह आपकी कॉल है, बेशक :) मैंने जवाब को थोड़ा सा और दस्तावेज के बाएं संदर्भों को संपादित किया है, यह बताते हुए कि मल्टी-टच सक्षम करने के लिए यह महत्वपूर्ण है या कोड काम नहीं करेगा (हम हमेशा इसके बारे में भूल जाते हैं!)। मैं इस उत्तर को अन्य लोगों के संदर्भ के रूप में छोड़ रहा हूं जिन्हें उस विवरण की आवश्यकता हो सकती है :) – Para

1

स्विफ्ट 1.2 में यह बदल गया है, और touchesBegan अब NSObjects का एक सेट प्रदान करता है।

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

क्या मुझे इस उत्तर को प्रश्न के उत्तर के रूप में चिह्नित करना चाहिए? (चूंकि अब चिह्नित उत्तर वास्तव में कोई जवाब नहीं है) – Fr4nc3sc0NL

0

स्विफ्ट 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) 
    } 
} 

संपादित करें, मेरा बुरा है, वह नहीं है उन के माध्यम से पुनरावृति करने के लिए, इस प्रकार के रूप में UITouch का एक सेट वस्तुओं छूता संग्रह डाली आपके सवाल का जवाब देने, एक ही समस्या थी और किसी this previous answer करने के लिए मुझसे जुड़ा हुआ: वैसे भी

, मैं इसे तेजी से 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 
      } 
     } 
    } 
} 

मुझे अभी भी एक छोटी सी समस्या है लेकिन मुझे लगता है कि यह मेरे कोड में मेरे जेस्चर रिकॉग्नाइज़र से जुड़ा हुआ है। लेकिन यह चाल करना चाहिए। यह आपको आपके कंसोल में प्रत्येक बिंदु के समन्वय को मुद्रित करेगा।

+0

अगर कोई यह पुष्टि कर सकता है कि यह काम कर रहा है, तो मैं इसे उत्तर के रूप में चिह्नित करूंगा – Fr4nc3sc0NL

+0

@ Fr4nc3sc0NL ने इसे सही बनाने के लिए अपना उत्तर संपादित किया। – Hawkydoky

0

स्विफ्ट 3,4 में अपने हैश द्वारा स्पर्श सूचक की पहचान:

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