5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【swift3】WKWebViewでUIGestureRecognizerを使う

Posted at

何かと使う機会が多そうなので...:confused:

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")
    }
    
}

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?