Search code examples
ipaduiviewcontrolleruibuttonuigesturerecognizeruitouch

Get the coordinates of a touch and place a button on that position?


I am trying to get the coordinates from a touch but I can't figure out how.
I am in a view controller and I try to use the method -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event but I think it doesn't work with view controllers.
Am I right?

I just want to put a Button on the position I touched.

Thanks in advance!


Solution

  • It belongs in a View rather than ViewController, and would look something like this:

    -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)evt {
      UITouch *touch=[touches anyObject];
      CGPoint pt=[touch locationInView:self];
      // ...make your button at 'pt'...
    }