import WebKit final class LimitedWKWebView: WKWebView { // storyboard required init?(coder: NSCoder) { super.init(coder: coder) // 選択をさせない navigationDelegate = self self.scrollView.delegate = self // 平行のインジケーター出さない self.scrollView.showsHorizontalScrollIndicator = false } override func target( forAction action: Selector, withSender sender: Any? ) -> Any? { return nil } override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { return false } } extension LimitedWKWebView: WKNavigationDelegate { // https://stackoverflow.com/questions/48467723/how-to-disable-user-selection-in-a-wkwebview-in-swift func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { let javascriptStyle = "var css = '*{-webkit-touch-callout:none;-webkit-user-select:none}'; var head = document.head || document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css'; style.appendChild(document.createTextNode(css)); head.appendChild(style);" webView.evaluateJavaScript(javascriptStyle, completionHandler: nil) } } extension LimitedWKWebView: UIScrollViewDelegate { // Zoomいらない func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) { scrollView.pinchGestureRecognizer?.isEnabled = false } // 横スクロールいらない func scrollViewDidScroll(_ scrollView: UIScrollView) { if (scrollView.contentOffset.x > 0){ scrollView.contentOffset = CGPoint(x: 0, y: scrollView.contentOffset.y) } } }
混乱するBoolのoptional
混乱するので
struct Klass { var a: Bool? = nil var b: Bool? = nil } let klass: Klass? = Klass(a: false, b: nil) let hoge = 3 if klass?.a == false { print("aはfalse") } if hoge == 3 && klass?.a == false { print("aはfalse") } if klass?.b == false { print("bはfalse") } if hoge == 3 && klass?.b == false { print("bはfalse") }
結果は。
aはfalse aはfalse