我有一个UIButton
,它的大小与视图相同(覆盖整个视图/屏幕)。
我已经设置了一个Touchdown操作,我需要在此操作中找出按钮上点击/按下的位置(获取x和y坐标)。
这可能吗?
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))
}
}
被接受的答案的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)")
}
}
beater
类中名为userbeat
的函数,但是该函数并不存在或者beater
对象已被释放。 - Ron Fessler// 获取按钮视图上的任何触摸 if let touch = event.touchesForView(buttonView)?.anyObject() as? UITouch { // 在按钮上打印触摸位置 println("这是什么\(touch.locationInView(buttonView))") }
} - Roi Mulia