UIButton TouchUpInside Touch Location
Asked Answered
P

2

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.

Peritonitis answered 10/9, 2011 at 21:52 Comment(3)
There is no touchUpLocation, UIButton is a control you can not get the location of UIButton click. developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/…Airport
I didn't mean literally touchUpLocation, I meant something like thatPeritonitis
@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.Thatcher
T
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);
}
Thatcher answered 11/9, 2011 at 1:37 Comment(7)
Thanks that completely solved my problem. I thought it couldn't be done, so this is great.Peritonitis
What a great snippet of code - sometimes these things show up which reaffirm your belief in human nature :-)Natty
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!Adamis
@Adamis 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.Thatcher
but if you got multiple touches, this doesn't get you any furtherLn
@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.Thatcher
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 somethingLevorotation
M
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)")  
}
Museum answered 27/3, 2017 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.