Last active
November 17, 2018 01:40
-
-
Save freddi-kit/12d6363a3df2acd2ed0941e6890aca5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TouchWebViewController.swift | |
// | |
// Created by 秋勇紀 on 2018/11/17. | |
// Copyright © 2018 勇者野良猫の部屋. All rights reserved. | |
// | |
import UIKit | |
import WebKit | |
class TouchWebViewController: UIViewController { | |
// MARK: Stored properties | |
private let googleUrl = URL(string: "https://google.com")! | |
// MARK: IBOutlet | |
@IBOutlet weak var webView: WKWebView! { | |
didSet { | |
// Set up web view | |
webView.load(URLRequest(url: googleUrl)) | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// UITapGestureRecognizerでタップイベントを取るようにする | |
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:))) | |
tapRecognizer.numberOfTapsRequired = 1 | |
// このときUIGestureRecognizerDelegateに準拠したオブジェクトを設定する | |
tapRecognizer.delegate = self | |
webView.addGestureRecognizer(tapRecognizer) | |
} | |
// MARK: Private functions | |
@objc private func handleTap(_ sender: UITapGestureRecognizer){ | |
NSLog("Tap") | |
} | |
} | |
extension TouchWebViewController: UIGestureRecognizerDelegate { | |
// MARK: UIGestureRecognizerDelegate | |
// このgestureRecognizerをオーバーライドしてtrueにしないとサブビューではTapイベントを検知できない | |
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://qiita.com/ruwatana/items/16997b1b416512c20fb6#%E5%90%8C%E6%99%82%E8%AA%8D%E8%AD%98%E5%88%B6%E5%BE%A1%E7%B3%BB%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89
参考文献