我将一个UIButton扩展到视图的大小(覆盖视图/屏幕)。
我设置了一个触地操作,我需要在这个动作中找出按钮上的点击/按下位置(获取x和y坐标)。
有可能吗?
发布于 2014-11-18 19:29:08
使用sender和event参数创建动作函数。然后你就可以得到按钮上的触摸位置。
@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))
}
}发布于 2017-06-29 15:01:19
Swift 3接受答案的版本:
@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)")
}
}https://stackoverflow.com/questions/26994399
复制相似问题