1
我有一個大按鈕,當用戶點擊我想找到他們的手指的位置。我熟悉從TouchesBegan方法中使用UITouches找到水龍頭的位置。有沒有辦法從UIButton獲取位置?觸摸位置使用按鈕swift 3
我的代碼:
@IBAction func buttonPressed(_ sender: UIButton) {
//Find touch location here
}
我有一個大按鈕,當用戶點擊我想找到他們的手指的位置。我熟悉從TouchesBegan方法中使用UITouches找到水龍頭的位置。有沒有辦法從UIButton獲取位置?觸摸位置使用按鈕swift 3
我的代碼:
@IBAction func buttonPressed(_ sender: UIButton) {
//Find touch location here
}
你需要繼承的UIButton
並使用touchesBegan
你可以觸摸相對於按鍵本身的位置或相對於上海華
class TouchReporterButton: UIButton {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let touch = touches.first
let location = touch?.location(in: self.superview);
if(location != nil)
{
//Do what you need with the location
}
}
}
希望這有助於
你的位置是什麼意思? x,y座標?您是否想要在單個目標中添加更多觸控目標? –
你需要相對於按鈕本身或超級視圖的位置? –
是的x,y座標和相對於超級視圖是最好的 –