Reputation: 5896
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?
Upvotes: 3
Views: 2930
Reputation: 964
Swift 3 version of accepted answer:
@IBAction func increaseButtonTapped(sender: UIButton, event: UIEvent) {
// get any touch on the buttonView
let events = event.touches(for: sender)
for event in events! {
let location = event.location(in: sender)
print("\(location)")
}
}
Upvotes: 0
Reputation: 2789
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))
}
}
Upvotes: 3