有UIView drawRect发生在后台线程中 [英] Having UIView drawRect occur in a background thread

查看:348
本文介绍了有UIView drawRect发生在后台线程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个 UIView 子类,实现类似于 setNeedsDisplay 的方法,除了重绘,通常通过 drawRect:调用)将在后台线程中发生,而不是在当前更新周期结束时。



它可能被称为 setNeedsAsynchronousDisplay 。或者现有的 setNeedsDisplay 可能被劫持,并不会导致重绘在周期结束时,或任何,只要它允许重绘不发生在主线程阻塞屏幕更新



直到重绘完成,视图才能继续使用其当前绘制的表示。



谢谢!

解决方案

p>是的,这是可能的。您可能需要在后台生成内容视图作为图像,并将图像推入nsdictionary或数组。



因此,当您的背景正在生成图像时,只要通过渲染图像就可以以drawrect函数显示图像,只要图像已生成即可。



显示如何操作的WWDC视频: WWDC 2012会话211 - 在IOS上构建并发用户界面。以下是视频说明:


为了获得良好的用户体验,必须保持您的应用程序响应,同时呈现复杂的UI元素和流程数据。了解如何在UIKit层使用并发执行绘图和其他常见操作,而不会阻止用户交互。



I would like to have a UIView subclass that implements a method similar to setNeedsDisplay, except that redrawing (i.e., that would usually be called via drawRect:) will occur in a background thread sometime soonish, rather than at the end of the current update cycle.

It might be called setNeedsAsynchronousDisplay. Or the existing setNeedsDisplay could get hijacked and not cause redraw at the end of the cycle, or whatever, as long as it lets the redraw not happen on the main thread blocking screen updating an interaction until its completed.

Until the redraw occurs, the view can continue to use its current drawn representation.

Is something along these lines reasonably doable?

Thanks!

解决方案

Yes it is possible. You will probably need to generate the content view as an image in the background and push the images into a nsdictionary or array.

So while your background is generating the images you can just show the image in drawrect function by rendering the image, providing the image has been generated.

A WWDC video that shows how to do it: WWDC 2012 session 211 - Building Concurrent User Interfaces on IOS. Here is the video description:

For a great user experience, it's essential to keep your application responsive while it renders complex UI elements and processes data. Learn how to use concurrency at the UIKit layer to perform drawing and other common operations without blocking user interaction.

这篇关于有UIView drawRect发生在后台线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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