显示隐藏视图真的很慢 [英] Showing hidden view really slow

查看:108
本文介绍了显示隐藏视图真的很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的设置:

self.webView = WKWebView(frame: frame, configuration: WKWebViewConfiguration())
self.externView = UIView(frame: frame)
self.externWebView = WKWebView(frame: subFrame, configuration: WKWebViewConfiguration())
let StackView = UIStackView(frame: frame)


self.externView?.addSubview(StackView)
self.externView?.backgroundColor = UIColor.blackColor()

view.addSubview(self.webView!)
self.externView?.hidden = true
StackView.addSubview(self.externWebView!)
view.addSubview(self.externView!)

这会创建我想要的初始设置,这允许1个Web视图作为主视图,可以启动,外部视图可以渲染其他内容,这是最初隐藏的。

This creates the initial setup I want, this allows for 1 web view to act as the main one, which can launch and external one to render something else, that is initially hidden.

我遇到的问题是来自我正在调用的工作线程

The problem I'm running into, is that from a worker thread I'm calling

WKExteralLoader.externView!.hidden = false;
WKExteralLoader.webView!.hidden = true; 

最多可能需要30秒才能生效;然而,它的倒数几乎是瞬时的。

which can take up to 30 seconds to take effect; however, its inverse is nearly instantaneous.

WKExteralLoader.webView!.hidden = false;
WKExteralLoader.externView!.hidden = true;

任何可能导致此问题的想法?

Any ideas what could cause this problem?

谢谢!

推荐答案

@MobileMon告诉你如何解决问题,但不是为什么。

@MobileMon told you how to fix your problem, but not why.

UIKit不是线程安全的,这意味着必须从主线程进行所有UIKit调用。设置视图的隐藏标志是UIKit调用,因此需要从主线程完成。

The UIKit is not thread-safe, meaning that must make all UIKit calls from the main thread. Setting a view's hidden flag is a UIKit call, so it needs to be done from the main thread.

如果从后台操作UIView对象,则结果未定义。大多数情况下,结果是需要很长时间才能使更改生效(你所看到的),但有时根本不会发生变化,有时结果是奇怪的视觉效果,有时你的应用程序会崩溃。

If you manipulate UIView objects from a background thread results are undefined. Most of the time the result is that it takes a long time for the change to take effect (what you are seeing), but sometimes the change doesn't happen at all, sometimes the result is bizarre visual effects, and sometimes your app will crash.

每当你从后台线程操作UIView对象时,你需要将它包装在对 dispatch_async(dispatch_get_main_queue())的调用中与MobileMon的答案一样(或者其他一些导致代码从主线程运行的方法,但这是首选方式。)

Whenever you manipulate a UIView object from a background thread, you need to wrap it in a call to dispatch_async(dispatch_get_main_queue()) as in MobileMon's answer (Or some other approach that causes the code to be run from the main thread, but this is the preferred way.)

要说清楚,你说:


我正在运行的问题进入,是来自我正在调用的工作线程

The problem I'm running into, is that from a worker thread I'm calling

WKExteralLoader.externView!.hidden = false;
WKExteralLoader.webView!.hidden = true;

WKExteralLoader.externView!.hidden = false; WKExteralLoader.webView!.hidden = true;

您无法从工作线程执行UI代码。相反,你需要编写这样的代码:

You can't do UI code from a worker thread. Instead you need to write that code like this:

dispatch_async(dispatch_get_main_queue(), ^{
  WKExteralLoader.externView!.hidden = false;
  WKExteralLoader.webView!.hidden = true;
});

这样,您可以从工作线程将UI代码发送回主线程。

That way, from your worker thread you dispatch your UI code back to the main thread.

您可能有其他代码在您不知道的情况下尝试从后台线程进行UIKit调用。要检查,请添加以下行:

You may have other code that's trying to do UIKit calls from a background thread when you're not aware of it. To check, add the following line:

print("isMainThread = \(Thread.current.isMainThread)")

然后检查控制台中的打印内容。

And then check to see what's printing in the console.

这篇关于显示隐藏视图真的很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆