Swift javascript / ios bridge break刷卡手势识别器

时间:2016-12-05 19:31:56

标签: ios swift

我有一个滑动手势识别器,可以将工具栏关闭并显示在屏幕上。在我使用WKUserContentController()添加JavaScript到iOS桥之前,它工作得非常好。该桥工作,成功打印JavaScript消息,但滑动不再有效。但是,如果我评论Webview = WKWebView(frame: self.view.frame, configuration: configuration)view.addSubview(Webview),那么桥梁显然会被破坏,但手势识别器会起作用。我不能让它们同时工作。我哪里出错?

编辑:也许这是相关的:使用下面的代码,当加载WebView时,活动指示器不会出现。此外,如果我注释掉隐藏工具栏的行(从页面上刷开起点),工具栏应该出现,但它不会:

import UIKit
import WebKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate,
                           WKNavigationDelegate, WKScriptMessageHandler {

    let locationManager = CLLocationManager()

    @IBOutlet var Webview: WKWebView!
    @IBOutlet var ActInd: UIActivityIndicatorView!
    @IBOutlet var Toolbar: UIToolbar!

    override func viewDidLoad() {
        super.viewDidLoad()

        let controller = WKUserContentController()
        controller.add(self, name: "SwiftNative")
        let configuration = WKWebViewConfiguration()
        configuration.userContentController = controller

        // commenting out these two lines disables the bridge but activates swiping again
        Webview = WKWebView(frame: self.view.frame, configuration: configuration)
        view.addSubview(Webview)

        let url = URL(string: "https://www.bazleysawesomesite.com")
        let request = URLRequest(url: url!)
        Webview.load(request)

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()

        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.handleSwipes(_:)))
        let leftSwipe  = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.handleSwipes(_:)))

        rightSwipe.direction = .right
        leftSwipe.direction  = .left

        view.addGestureRecognizer(rightSwipe)
        view.addGestureRecognizer(leftSwipe)

        self.view.backgroundColor = UIColor.black
    }

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        print("Message from beyond: \(message.body)")
    }

    // Make the status bar text light
    override var preferredStatusBarStyle : UIStatusBarStyle {
        return UIStatusBarStyle.lightContent
    }

    // Start the toolbar "hidden"
    // commenting out these lines should make the toolbar appear, but it doesn't
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        Toolbar.center.x = Toolbar.center.x - self.view.frame.width
    }

    // Activity Indicator stuff
    func webViewDidStartLoad(_ : WKWebView) {
        ActInd.startAnimating()
    }
    func webViewDidFinishLoad(_ : WKWebView) {
        ActInd.stopAnimating()
        if self.Toolbar.center.x > 0 {
            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                self.Toolbar.center.x = self.Toolbar.center.x - self.view.frame.width
            }) 
        }
    }

    // handle swipes
    func handleSwipes(_ sender:UISwipeGestureRecognizer) {
        if (sender.direction == .right) {
            if self.Toolbar.center.x < 0 {
                UIView.animate(withDuration: 0.3, animations: { () -> Void in
                    self.Toolbar.center.x = self.Toolbar.center.x + self.view.frame.width
                }) 
            }
        }
        if (sender.direction == .left) {
            if self.Toolbar.center.x > 0 {
                UIView.animate(withDuration: 0.3, animations: { () -> Void in
                    self.Toolbar.center.x = self.Toolbar.center.x - self.view.frame.width
                }) 
            }
        }
    }

}

0 个答案:

没有答案