加载WebView加载时卡住/冻结的动画

时间:2019-02-27 10:09:54

标签: ios swift animation wkwebview

我加载进度加载动画,当来自Alamofire的响应出现时,我使用响应的一部分来构造完整的URL,我需要将其加载到wkwebview中,然后触发webview.load(..)。

我的问题是,一旦webview.load(..)开始发生,进度加载动画就被卡住了,一直停留到我hide()为止。

在网络视图开始加载页面的同时,我如何才能使动画继续移动?

MyViewController.swift

Rails.root: /Users/Richard/Code/whisper

Application Trace | Framework Trace | Full Trace
actionpack (5.2.0.rc2) lib/abstract_controller/helpers.rb:162:in `rescue in block in modules_for_helpers'
actionpack (5.2.0.rc2) lib/abstract_controller/helpers.rb:155:in `block in modules_for_helpers'
actionpack (5.2.0.rc2) lib/abstract_controller/helpers.rb:144:in `map!'
actionpack (5.2.0.rc2) lib/abstract_controller/helpers.rb:144:in `modules_for_helpers'
actionpack (5.2.0.rc2) lib/action_controller/metal/helpers.rb:94:in `modules_for_helpers'
actionpack (5.2.0.rc2) lib/abstract_controller/helpers.rb:108:in `helper'
actionpack (5.2.0.rc2) lib/abstract_controller/helpers.rb:186:in `default_helper_module!'
actionpack (5.2.0.rc2) lib/abstract_controller/helpers.rb:35:in `block in inherited'
actionpack (5.2.0.rc2) lib/abstract_controller/helpers.rb:35:in `class_eval'
actionpack (5.2.0.rc2) lib/abstract_controller/helpers.rb:35:in `inherited'
actionview (5.2.0.rc2) lib/action_view/layouts.rb:219:in `inherited'
actionpack (5.2.0.rc2) lib/action_controller/metal/rendering.rb:23:in `inherited'
actionpack (5.2.0.rc2) lib/action_controller/metal/parameter_encoding.rb:10:in `inherited'
actionpack (5.2.0.rc2) lib/action_controller/metal/params_wrapper.rb:237:in `inherited'
actionpack (5.2.0.rc2) lib/abstract_controller/railties/routes_helpers.rb:9:in `block (2 levels) in with'
actionpack (5.2.0.rc2) lib/action_controller/railties/helpers.rb:7:in `inherited'
app/controllers/companies_controller.rb:1:in `<main>'
bootsnap (1.3.2) lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
bootsnap (1.3.2) lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:468:in `block in load_file'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:653:in `new_constants_in'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:467:in `load_file'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:365:in `block in require_or_load'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:37:in `block in load_interlock'
activesupport (5.2.0.rc2) lib/active_support/dependencies/interlock.rb:14:in `block in loading'
activesupport (5.2.0.rc2) lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
activesupport (5.2.0.rc2) lib/active_support/dependencies/interlock.rb:13:in `loading'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:37:in `load_interlock'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:348:in `require_or_load'
bootsnap (1.3.2) lib/bootsnap/load_path_cache/core_ext/active_support.rb:46:in `block in require_or_load'
bootsnap (1.3.2) lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
bootsnap (1.3.2) lib/bootsnap/load_path_cache/core_ext/active_support.rb:45:in `require_or_load'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:502:in `load_missing_constant'
bootsnap (1.3.2) lib/bootsnap/load_path_cache/core_ext/active_support.rb:58:in `block in load_missing_constant'
bootsnap (1.3.2) lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
bootsnap (1.3.2) lib/bootsnap/load_path_cache/core_ext/active_support.rb:57:in `load_missing_constant'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:193:in `const_missing'
activesupport (5.2.0.rc2) lib/active_support/inflector/methods.rb:283:in `const_get'
activesupport (5.2.0.rc2) lib/active_support/inflector/methods.rb:283:in `block in constantize'
activesupport (5.2.0.rc2) lib/active_support/inflector/methods.rb:281:in `each'
activesupport (5.2.0.rc2) lib/active_support/inflector/methods.rb:281:in `inject'
activesupport (5.2.0.rc2) lib/active_support/inflector/methods.rb:281:in `constantize'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:574:in `get'
activesupport (5.2.0.rc2) lib/active_support/dependencies.rb:605:in `constantize'
actionpack (5.2.0.rc2) lib/action_dispatch/http/request.rb:88:in `controller_class_for'
actionpack (5.2.0.rc2) lib/action_dispatch/http/request.rb:81:in `controller_class'
actionpack (5.2.0.rc2) lib/action_dispatch/routing/route_set.rb:46:in `controller'
actionpack (5.2.0.rc2) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.2.0.rc2) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.0.rc2) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.0.rc2) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.0.rc2) lib/action_dispatch/routing/route_set.rb:840:in `call'
warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
warden (1.2.8) lib/warden/manager.rb:34:in `catch'
warden (1.2.8) lib/warden/manager.rb:34:in `call'
rack (2.0.6) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.0.6) lib/rack/etag.rb:25:in `call'
rack (2.0.6) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.6) lib/rack/head.rb:12:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.0.6) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.6) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/cookies.rb:670:in `call'
activerecord (5.2.0.rc2) lib/active_record/migration.rb:559:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.0.rc2) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.0.rc2) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.0.rc2) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.0.rc2) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.0.rc2) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.0.rc2) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.0.rc2) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.6) lib/rack/method_override.rb:22:in `call'
rack (2.0.6) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.0.rc2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.0.rc2) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.6) lib/rack/sendfile.rb:111:in `call'
webpacker (3.5.5) lib/webpacker/dev_server_proxy.rb:22:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (5.2.0.rc2) lib/rails/engine.rb:524:in `call'
puma (3.12.0) lib/puma/configuration.rb:225:in `call'
puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
puma (3.12.0) lib/puma/server.rb:472:in `process_client'
puma (3.12.0) lib/puma/server.rb:332:in `block in run'
puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'

Loading.swift

class MyViewController: UIViewController, WKScriptMessageHandler {

    var webView: WKWebView?

    @IBOutlet weak var webViewContainer: UIView!

    var webConfig:WKWebViewConfiguration {
        get {
            let webCfg:WKWebViewConfiguration = WKWebViewConfiguration()

            let userController:WKUserContentController = WKUserContentController()
            userController.add(self, name: "mycontroller")

            webCfg.userContentController = userController;

            return webCfg;
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        webView = WKWebView (frame: webViewContainer.bounds, configuration: webConfig)
        webView!.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        webView?.scrollView.isScrollEnabled = false
        webViewContainer.addSubview(webView!)

        loadWebview()
    }

    func loadWebview(){
        Loading.shared.show(self.view)

        Alamofire.request(MYAPI, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil)
            .responseJSON { response in
                let url = URL(string: "https://path-to-load/\(response.key)")
                    self.webView!.load(URLRequest(url: url!))
        }

    }

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        let message = message.body as! [String:AnyObject]
        let event = message["event"] as? String ?? "empty"

        switch (event){
        case "loading-finished":
            DispatchQueue.main.async {
                Loading.shared.hide(animated: true)
            }
            break
        default:
            break
        }
    }


}

1 个答案:

答案 0 :(得分:1)

可能的解决方案:

1)当您希望全屏加载时(在默认情况下,inview == false)在应用程序窗口中进行加载,并将loadWebview()保留在viewDidLoad中

public func show(_ view: UIView? = nil, inView: Bool = false) {
        var window: UIView!
        if inView == false, let w = UIApplication.shared.delegate?.window {
            window = w
        } else {
            window = view
        }
     ...

2)将loadWebview()从viewDidLoad移至viewDidAppear

要移到这里的重要部分是Loading.shared.show(self.view)。在布局完成之前,我无法对视图的各个组件进行动画处理(这恰好发生在viewDidAppear()中)。

更多详细信息: viewDidLoad在MyViewController初始化并初始化主视图之后但在将视图呈现给UI之前调用。这意味着viewDidLoad允许我设置ViewController和View,然后再显示给用户进行交互。

尽管这是一个不错且快速的解决方案,但在某些情况下可能无法按预期工作,因为 viewDidAppear可以调用两次,,因此两次显示加载视图,这将导致奇怪的用户体验。