何かと使う機会が多そうなので...
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")
}
}