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

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

问题描述

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

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.

它可能被称为setNeedsAsynchronousDisplay.或者现有的 setNeedsDisplay 可能会被劫持,并且不会在循环结束时导致重绘,或者其他什么,只要它让重绘不会发生在主线程阻塞屏幕上,更新交互直到它完成.

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?

谢谢!

推荐答案

是的,这是可能的.您可能需要将内容视图生成为背景中的图像并将图像推送到 nsdictionary 或数组中.

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.

因此,当您的背景生成图像时,您可以通过渲染图像在 drawrect 函数中显示图像,前提是图像已生成.

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.

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

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

要获得出色的用户体验,在呈现复杂的 UI 元素和处理数据时保持应用程序响应至关重要.了解如何在 UIKit 层使用并发来执行绘图和其他常见操作,而不会阻止用户交互.

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天全站免登陆