對所有類型的視圖實現UITapGestureRecognizer
將是一個解決方法。
override func viewDidLoad(){
super.viewDidLoad()
let tapRecognizer = UITapGestureRecognizer(target: view, action: "handleSingleTap:")
tapRecognizer.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tapRecognizer)
}
func handleSingleTap(recognizer: UITapGestureRecognizer) {
//Do something here with the gesture
}
對於觸摸開始和觸摸結束通過重寫本機方法嘗試使用該代碼。
override func touchesBegan(touches: Set, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
let touch: UITouch = touches.first as! UITouch
if (touch.view == yourView){
println("touchesBegan | This is an ImageView")
}else{
println("touchesBegan | This is not an ImageView")
}
}
touchend func。
override func touchesEnded(touches: Set, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
let touch: UITouch = touches.first as! UITouch
if (touch.view == yourView){
printing("touchesEnded | This is an ImageView")
}
else{
println("touchesEnded | This is not an ImageView")
}
}
你能證明你有什麼之前試過.. – vaibhav