Search code examples
iosswift2uitouchtouchesbegan

Value of type 'UITouch' has no member 'locationInNode'


I'm trying to get location of touch with swift2.1 and get error "Value of type 'UITouch' has no member 'locationInNode'".

here is my code

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
        let location = touch.locationInNode(self)
        print(location)
    }
}

I had a look into source and saw that in class UITouch there is only public

func locationInView(view: UIView?) -> CGPoint {
}

is it something I should use or ... ?


Solution

  • As NicolasMiari pointed out in the comment, only if you are using SpriteKit this will work. locationInNode() is a SpriteKit function that helps you find the point in an SKNode.

    If you are using "standard" UIKit where you want to get the location of a touch in a view, you should be using locationInView() which takes a view(!) not the controller itself.

    class ViewController : UIViewController{
    
      override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
          let location = touch.locationInView(self.view)
          print(location)
        }
      }
    
    }