-1

Hey in trying to track multiple touches and i get this error. And I know why i get the error its because of the Set at the top of the touchesbegin function. but i have to keep my Set in the overrideBegin. so how would i solve this error and make this code error free with out changing the Orignal override values.

enter image description here

Code:

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


    var touchesArray = touches.allObjects()
    var nNumTouches = touchesArray.count
    var touch: UITouch!
    var ptTouch = CGPoint.zero
    for nTouch in 0..<nNumTouches {
        touch = touchesArray[nTouch]
        ptTouch = touch.locationInView(self.view)
        //I need it to print the location of each individual touch like finger 1 or finger 2 is in this location
    }

  }
Hunter
  • 107
  • 1
  • 10
  • 1
    There should be no need to convert the set to an array, you can iterate over a set: `for touch in touches { ... }` – Martin R Oct 02 '16 at 20:35

3 Answers3

1

It's Swift's Set, not NSSet. Try:

let touchesArray = Array(touches)

But I see that there's no need for such conversion here because you can iterate over set. Try only this:

for touch in touches {
    let point = touch.location(in: self.view)
    print("x: \(point.x), y: \(point.y)")
}
Kubba
  • 3,390
  • 19
  • 34
  • It works Awesome! I will mark as answer when it lets me. Also if i wanted to print the location of each touch which in sure is easy. Then how would i do that? it would help alot @Kubba – Hunter Oct 02 '16 at 20:36
  • print(String(format: "x=%.2f y=%.2f", ptTouch.x, ptTouch.y)) this work for tracking one touch – Hunter Oct 02 '16 at 20:56
0

Why just dont make something like this? Does your stuff in loop require counter?

let touchesArray = touches
var ptTouch: CGPoint?
for touch in touchesArray {
    ptTouch = touch.locationInView(self.view)

    print(ptTouch)
}
Lu_
  • 2,577
  • 16
  • 24
  • ok i will try but really im trying to just print the location of like the 1st touch then like if i wanted to. print the location of the 2,3 or 4 touch. You know like track there location. so basically if the can show me how to simply print the location of like touch 2/ finger 2 then ill mark as Answer and it would help alot! – Hunter Oct 02 '16 at 20:45
0

You can use .count on touches without converting it to an array. but there's no need as you can iterate over the touches set.

To be able to register multiple touches you need to add this line to your viewDidLoad() method: view.multipleTouchEnabled = true

And modify touchesBegan like this:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for (index,touch) in touches.enumerate() {
        let ptTouch = touch.locationInView(self.view)
        print("Finger \(index+1): x=\(pTouch.x) , y=\(pTouch.y)")
    }

}
Swifty
  • 3,730
  • 1
  • 18
  • 23
  • Ok Im trying to track multiple touches so i can basically constantly run a line of code that will print the location of like touch 2 / finger 2. but i dont know how to print that – Hunter Oct 02 '16 at 20:49
  • @Hunter Do you mean that you want to print the location of both fingers when you tap whit two fingers? – Swifty Oct 02 '16 at 21:06
  • yes that's exactly what im trying to do but basically so i want the touch 1 to have its own location and i want touch 2 to have its own. so then basically in touchesdidMove i can track touch 1's location if it moves and i can individually track touch 2's location in moved. by simply printing if touches moved the give me the location of touch 1 which is still on the screen and give me the location touch 2 which is still being drag on the screen – Hunter Oct 02 '16 at 21:16
  • Ok Almost there I basically need to say let finger1 = index[1] basically saying let finger1 = touch number 1 or let it equal the first finger that was touched the screen & let finger2 = touch 2 or the second finger that was touched the screen – Hunter Oct 02 '16 at 21:28
  • @Hunter If you need to keep track of the position of multiple fingers individually in your `touchesMoved` method, then i suggest you ask another question with the current code you have for `touchesBegan` and `touchesMoved`. Because it is out of the scope of this question. I'll be happy to take a look. – Swifty Oct 02 '16 at 23:17
  • http://stackoverflow.com/questions/39823309/how-to-keep-track-of-the-position-of-multiple-fingers-individually – Hunter Oct 03 '16 at 01:08
  • Ok i asked a new question the link is at the comment above this one @Sam_M – Hunter Oct 03 '16 at 01:08
  • @Hunter I was just about to post an answer there, Why did you delete it? – Swifty Oct 03 '16 at 01:33
  • Sorry it was getting down voted and to many down votes on here and they'll ban you in a heart beat. ill repost the question ill put the link below this comment thanks for helping me out. give me a sec @Sam_M – Hunter Oct 03 '16 at 02:14
  • I can only post every 90minutes so i have like 15minutes to go so i give a notification which will be the link in a comment to it @Sam_M – Hunter Oct 03 '16 at 02:19
  • http://stackoverflow.com/questions/39823914/how-to-track-multiple-touches – Hunter Oct 03 '16 at 02:42