预加载 UIWebView,避免白屏闪烁
我正在开发一个具有表格导航的应用程序,最终可以深入到显示各种信息的 UIWebView
。然而,从流畅的 UITableView
到缓慢的 UIWebView 的过渡对用户来说是不舒服的,所以我想尽我所能地改善这种体验。
具体来说,tableViews
和 UIWebView
页面的背景都是黑色背景,但是当我打开 UIWebView
时,它会闪烁空白色大约一秒钟(本地和远程 HTML 文件都是这种情况。)我如何(理想地)预加载此过程,或者(至少)使“flash”全黑而不是全白?我尝试将视图/webView 的背景设为黑色,但这似乎没有帮助。
现在在我的应用程序中,当用户选择一个单元格时,应用程序仅加载 UIWebView 子类并将其推送到导航堆栈上。 UIWebView
子类有一个启动并显示活动指示器。在 WebViewDidStartLoad
和 WebViewDidFinishLoad 上停止动画,效果很好,但它对“白色闪光”没有任何帮助。
I'm working on an app that has table navigation to eventually drill down to UIWebView
that displays various information. However, the transition from the slickness of UITableView
to the slow wonkiness of UIWebView
is jarring for the user so I want to improve that experience however I can.
Specifically, the background of both the tableViews
and UIWebView
pages have black backgrounds, but when I open the UIWebView
it flashes empty white for about a second (this is the case for both local and remote HTML files.) How can I (ideally) preload this process, or (at least) make the "flash" be all black rather than all white? I tried making the view / webView's background black but that didn't seem to help.
In my app right now, when a user selects a cell, the app just loads up the UIWebView
subclass and pushes it on the navigation stack. The UIWebView
subclass has an activity indicator that starts & stops animating on WebViewDidStartLoad
and WebViewDidFinishLoad, which works fine, but it doesn't do anything to help the "white flash."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我已经测试过了...我正在发送我使用过的两种方法...
这里我在 0.1 秒后执行请求,否则它会像您的情况一样看起来是白色的。或者您可以根据时间给出延迟时间..干杯:)
I have tested it... I'm sending the two method that I have used...
here I'm performing the request after 0.1 sec, other wise it will look white as in your case. Or you can give your delay time depending upon the time.. Cheers :)
尝试在你的
然后在
Try in your
Then in
我使用:
在创建 webView 并发出请求时,然后添加此委托方法来处理已完成的请求:
I used:
when creating my webView and making the request and then added this delegate method to handle completed requests:
成功地做到了。
您必须首先在 Interface Builder 中创建一个视图。
然后使用包含 webview 的 ViewController 的 init 中的 initWithFrame 将 html 加载到 webview(这就是神奇发生的地方):
然后简单地将 webView 加载到 viewWillAppear 中的视图中:
这实际上是一个界面设计问题,其中直接在视图上绘制或在子视图中绘制然后在视图中绘制该子视图更快吗?
Successfully done it.
You have to create a view in Interface Builder first.
Then load the html to the webview using a initWithFrame in the init of your ViewController that contains the webview(this is where the magic happens):
Then simply load the webView into the view in viewWillAppear:
This is really a question of interface designing, which is faster paint directly on the view or paint in a subview and then paint that subview in the view?
我几年前就解决了这个问题,使用常见的方法将 UIWebView 隐藏在 UIImageView 后面,然后在延迟后删除 UIImageView。
但它突然又回来了,我想是在 iOS 7.0.4 上。该问题发生在全新的 iPad Air 以及较旧的非视网膜版 iPad mini 上。经过两天的折腾,终于找到了解决办法。
假设您的 webview 仅限于横向,初始化如下:
然后在预加载后使其可见,例如在 webview 上使用bringSubviewToFront或setHidden:NO(或者在UIImageView上使用setHidden:YES或removeFromSuperview) 。但不是无缝切换视图,而是闪烁并且背景颜色闪烁大约半秒。
解决方法是稍微改变你的 webview 的大小:
问题和修复是高度可重现的。它的工作原理是小数点后第四位 (1024.0001f) 的微小变化。在小数点第五位 (1024.00001f) 处,闪光灯返回。高度的值(768.0f)似乎并不重要。
I had solved this problem years ago, using the common method of hiding the UIWebView behind a UIImageView, then removing the UIImageView after a delay.
But it suddenly came back, I think on iOS 7.0.4. It was occurring on a brand new iPad Air, as well as an older iPad mini non-retina. After two days of hair-pulling, I finally found a work-around.
Let's say you have webview which is restricted to landscape orientation, initialized like this:
And then you make it visible after pre-loading, with eg bringSubviewToFront or setHidden:NO on the webview (or alternatively with setHidden:YES or removeFromSuperview on the UIImageView). But instead of seamlessly switching the views, there's a flash and the background color blinks for about half a second.
The fix is to change the size of your webview, ever so slightly:
The problem and fix is highly reproducible. It works with a change as slight as the fourth decimal place (1024.0001f). At the fifth decimal place (1024.00001f), the flash returns. The value of the height (768.0f) didn't seem to matter.
或者,您可以将 webview 的背景颜色设置为您正在使用的任何背景颜色,而不是解决该问题。当然,除非您使用图像。
Or, instead of working around the problem, you could just set the background color of your webview to whatever background color you're using. Unless you're using an image of course.