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

前端 未结 3 642
[愿得一人]
[愿得一人] 2020-12-16 19:09

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

3条回答
  •  一整个雨季
    2020-12-16 19:49

    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));
    }
    

提交回复
热议问题