Search code examples
iosswiftswift2uitouchcgpoint

Error with "touchesBegan"


My code:

var location = CGPoint(x:0,y:0) 
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch = touches.anyObject() as? UITouch 
    location = touch.locationInView(self.view) 
    Button.center = location 
}

Gives an error on this line:

let touch = touches.anyObject() as? UITouch 

How to fix it?


Solution

  • Try this : - 
    
      override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    
        for touch in touches {
        if let touch = touches.first as? UITouch {
                               if touch.tapCount == 2{
                                    //Do Something here
                                }
                                                 }
                              }
            }