2016-12-31 79 views
1

我想在我的swift 3應用程序中包含一個webview觸摸事件,但這樣做時仍然不成功。Swift 3的webview觸摸事件

我嘗試使用toucheBegan和touchesEnd事件通過搜索google搜索到的例子。可有人過去的片段或通過這種方式

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){ 
    super.touchesBegan(touches, with: event) 
} 

,但不工作與SWIFT 3

我想要的鏈接。我不知道如何在其中集成webview。

+0

你能證明你有什麼之前試過.. – vaibhav

回答

2

對所有類型的視圖實現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") 
    } 
} 
+0

沒有工作對我來說 –

+0

我編輯的問題,PLZ檢查 –

+0

代碼肯定的作品,你需要實現由執行觸摸開始和觸摸到底是什麼。 – vaibhav