35

So I have a large UIButton, it is a UIButtonTypeCustom, and the button target is called for UIControlEventTouchUpInside. My question is how can I determine where in the UIButton the touch occured. I want this info so I can display a popup from the touch location. Here is what I've tried:

UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

and various other iterations. The button's target method get info from it via the sender:

    UIButton *button = sender;

So is there any way I could use something like: button.touchUpLocation?

I looked online and couldn't find anything similar to this so thanks in advance.

Andrew
  • 3,874
  • 5
  • 39
  • 67
  • There is no touchUpLocation, UIButton is a control you can not get the location of UIButton click. http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIButton_Class/UIButton/UIButton.html – Praveen-K Sep 10 '11 at 22:03
  • 1
    I didn't mean literally touchUpLocation, I meant something like that – Andrew Sep 10 '11 at 22:09
  • 3
    @Praveen-K, that's incorrect. UIKit supports actions that take a second parameter, the UIEvent which triggered the action. From that you can get the list of touches, and each touch has a location. See the code I posted below. It's true that UIButton doesn't have a method that returns the location of the last click, however. – Caleb Sep 11 '11 at 01:40

2 Answers2

61
UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

That's the right idea, except that this code is probably inside an action in your view controller, right? If so, then self refers to the view controller and not the button. You should be passing a pointer to the button into -locationInView:.

Here's a tested action that you can try in your view controller:

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
{
    UIView *button = (UIView *)sender;
    UITouch *touch = [[event touchesForView:button] anyObject];
    CGPoint location = [touch locationInView:button];
    NSLog(@"Location in button: %f, %f", location.x, location.y);
}
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    Thanks that completely solved my problem. I thought it couldn't be done, so this is great. – Andrew Sep 11 '11 at 03:13
  • 1
    What a great snippet of code - sometimes these things show up which reaffirm your belief in human nature :-) – SomaMan Mar 02 '12 at 09:44
  • Is there a way to get the event from `- (IBAction) buttonPressed: (id) sender` ...? (That is, I have a huge project and I'd like to bottleneck this, without changing all the buttons to `...forEvent:`, if I can.) Thanks! – Olie Nov 07 '13 at 22:36
  • @Olie As far as I know, if you want the event you'll need to change the signature of the action to include the event parameter. – Caleb Nov 08 '13 at 03:00
  • but if you got multiple touches, this doesn't get you any further – Peter Lapisu Dec 11 '13 at 16:06
  • @PeterLapisu Of course it does. The point of the answer is that the OP needed to specify the button rather than `self` in the call to `-locationInView:`. I used `-anyObject` because that's what the OP used, but there's nothing to prevent you from looking at each touch in the set of touches: `NSSet *touches = [event touchesForView:button]; for (UITouch *t in touches) {CGPoint p = [t locationInView:button]; ...}`. But, again, the OP's question isn't about how to handle multiple touches. – Caleb Dec 11 '13 at 17:38
  • Great! This answer save me a lot of time!By the way,if you want location in the view controller that the button belongs,you can:CGPoint location = [touch locationInView:self.view]; or something – inix Apr 20 '16 at 02:34
7

For Swift 3.0:

@IBAction func buyTap(_ sender: Any, forEvent event: UIEvent) 
{
       let myButton:UIButton = sender as! UIButton
       let touches: Set<UITouch>? = event.touches(for: myButton)
       let touch: UITouch? = touches?.first
       let touchPoint: CGPoint? = touch?.location(in: myButton)
       print("touchPoint\(touchPoint)")  
}
Ammar Mujeeb
  • 1,222
  • 18
  • 21