[swift3] WKWebView에서 UIGesture Recognizer 사용
WebviewController.swift
import UIKit
//WebKit Frameworkをインポート
import WebKit
class WebviewController: UIViewController, UIGestureRecognizerDelegate {
//WKWebviewの宣言
var _webkitview: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//WebKitのインスタンス作成
self._webkitview = WKWebView()
//WebKitをviewに紐付け
self.view = self._webkitview!
// ジェスチャーを生成(今回はタップ・スワイプ・長押し)
let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(WebviewController.tap(_:)))
let swipeGesture:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(WebviewController.swipe(_:)))
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(WebviewController.longPress(_:)))
// デリゲートをセット
tapGesture.delegate = self;
swipeGesture.delegate = self;
longPressGesture.delegate = self;
// WebViewに追加
self._webkitview!.addGestureRecognizer(tapGesture)
self._webkitview!.addGestureRecognizer(swipeGesture)
self._webkitview!.addGestureRecognizer(longPressGesture)
//URLを作って表示
var url:NSURL
url = NSURL(string:"http://******")!
let req:NSURLRequest;
req = NSURLRequest(url:url as URL, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData, timeoutInterval: 0)
self._webkitview!.load(req as URLRequest)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func gestureRecognizer(
_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith
otherGestureRecognizer: UIGestureRecognizer
) -> Bool {
return true
}
func tap(_ sender: UITapGestureRecognizer){
//タップ時の処理
print("tap")
}
func swipe(_ sender: UITapGestureRecognizer){
//スワイプ時の処理
print("swipe")
}
func longPress(_ sender: UITapGestureRecognizer){
//長押し時の処理
print("longPress")
}
}
Reference
이 문제에 관하여([swift3] WKWebView에서 UIGesture Recognizer 사용), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/KOH_TA/items/769fda8b9c7d19e991e0텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)