-1

I am trying to enable touch on pieces of a sliding tile puzzle so they can each move around the screen.
I want to assign the user's first tap on the screen to myTouch, then perform some action. In older code, [[touches allObjects] objectAtIndex:0] is what I needed, but not sure how to make this line of code work in Swift.

var tapCen = CGPoint();
var left = CGPoint();
var right = CGPoint();
var top = CGPoint();
var bottom = CGPoint();

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    let allTouches = event?.allTouches()
    let myTouch = allTouches?.first

    if myTouch!.view != self.view
    {
        tapCen = myTouch!.view!.center

    }
}
  • Possible duplicate of ['Set' does not have a member named 'anyObject." - Xcode 6.3](http://stackoverflow.com/questions/29584365/setnsobject-does-not-have-a-member-named-anyobject-xcode-6-3) – jtbandes Apr 18 '16 at 02:32
  • Question reworded and reformatted with more clear problem statement. – James D. Schwartz Apr 18 '16 at 15:32
  • What's wrong with your code as written? – jtbandes Apr 18 '16 at 16:28
  • @jtbandes Thanks for taking a look at my question! Before I got to try the answer Rajan posted, my question got put on hold. Guess it was too vague... I included a lot of extra code only because in the past people said I didn't post enough! I lost some reputation, so I'm trying to correct my ways. :) I'm new at this stackoverflow thing. – James D. Schwartz Apr 19 '16 at 01:27
  • 1
    You can take a look at question guidelines on the help center: [ask] and [mcve]. The important part is that it should be clear what's going wrong. There's no indication in this question of what the error is, on which line, or what you're having trouble with. – jtbandes Apr 19 '16 at 02:29

1 Answers1

1

You can try this

let allTouches = event.allTouches()
let myTouch = allTouches?.first

Edit
Swift 3.0

let allTouches = event?.allTouches
let myTouch = allTouches?.first
Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98