我有一个 UIButton
拉伸(stretch)到 View 的大小(覆盖 View /屏幕)。
我已经设置了一个 Touchdown Action ,我需要在这个 Action 中找出点击/按下按钮的位置(获取 x 和 y 坐标)。
这可能吗?
最佳答案
使用 sender
和 event
参数创建 Action 函数。然后就可以得到按钮上的触摸位置。
@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))
}
}
关于swift - 在发送者操作方法中检测 UIButton 中点击/按下的位置是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26994399/