twbbas
twbbas

Reputation: 447

How do I get the tap coordinates on a custom UIButton?

I'm using XCode 4.4 developing for iOS 5 on an iPad and am using the Storyboard layout when creating my custom button.

I have the touch event correctly working and logging but now I want to get the x/y coordinates of the tap on my custom button.

If possible, I'd like the coordinates to be relative to the custom button instead of relative to the entire iPad screen.

Here's my code in the .h file:

- (IBAction)getButtonClick:(id)sender;

and my code in the .m file:

    - (IBAction)getButtonClick:(id)sender {

        NSLog(@"Image Clicked.");
    }

Like I said, that correctly logs when I tap the image.

How can I get the coordinates of the tap?

I've tried a few different examples from the internet but they always freeze when it displays a bunch of numbers (maybe the coordinates) in the log box. I'm VERY new to iOS developing so please make it as simple as possible. Thanks!

Upvotes: 12

Views: 10737

Answers (3)

Ammar Mujeeb
Ammar Mujeeb

Reputation: 1311

Incase of Swift 3.0 the accepted answer works same except syntax will be changed as follows:

Swift 3.0:

 @IBAction func buyTap(_ sender: Any, forEvent event: UIEvent) {
     let myButton = sender as! UIButton
     let touches = event.touches(for: myButton)
     let touch = touches?.first
     let touchPoint = touch?.location(in: myButton)
     print("touchPoint\(touchPoint)")
  }

Upvotes: 3

NeonBlueHair
NeonBlueHair

Reputation: 1159

For your overall coordinates (with reference to the screen), you need to create a CGPoint that contains the coordinates of your touch. But to do that, you need to get that touch first. So start by getting the touch event, then by making that point using the locationInViewmethod. Now, depending on when you want to log the touch - when the user touches down, or when they lift their finger -, you have to implement this code in the touchesBegan or touchesEnded method. Let's say you do touchesEnded, which passes an NSSet cales "touches" containing all the touch events.

UITouch *tap = [touches anyObject];
CGPoint touchPoint = [tap locationInView:self.view];

"touchPoint" will now contain the point at which the user lifts their finger. To print out the coordinates, you just access the x and y properties of that point:

CGFloat pointX = touchPoint.x;
CGFloat pointY = touchPoint.y;
NSLog(@" Coordinates are: %f, %f ", pointX, pointY);

That should output the coordinates of the touch. Now to have it be referenced to whatever button you're using, I would suggest you just manually subtract the values for the button's coordinates from the point. It seems like a simple solution, and honestly I don't know a way of getting coordinates with reference to another object, unless you make a view based on that object, and pass it to locationInView instead of self.view.

For more info on touches, there's a great set of tutorials here.

Upvotes: 0

Vladimir
Vladimir

Reputation: 170829

To get touch location you can use another variant of button action method: myAction:forEvent: (if you create it from IB interface note "sender and event" option in arguments field: enter image description here)

Then in your action handler you can get touch location from event parameter, for example:

- (IBAction)myAction:(UIButton *)sender forEvent:(UIEvent *)event {
    NSSet *touches = [event touchesForView:sender];
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:sender];
    NSLog(@"%@", NSStringFromCGPoint(touchPoint));
}

Upvotes: 31

Related Questions