Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect what is the location of the tap/press inside UIButton inside sender action method

I have a UIButton stretched to the size of the view (covers the view/screen).

I have set a Touchdown action and I need in this action to find out what is the location of the tap/press on the button (get x and y coordinates).

Is it possible?

like image 841
Roi Mulia Avatar asked Dec 02 '25 09:12

Roi Mulia


1 Answers

Create the action function with sender and event params. Then you can get the touch location on the button.

@IBAction func buttonAction(sender: AnyObject, event: UIEvent) {
    // downcast sender as a UIView
    let buttonView = sender as UIView;

    // get any touch on the buttonView
    if let touch = event.touchesForView(buttonView)?.anyObject() as? UITouch {
        // print the touch location on the button
        println(touch.locationInView(buttonView))
    }
}
like image 65
Ron Fessler Avatar answered Dec 06 '25 06:12

Ron Fessler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!