CATiledLayer 用按钮绘制巨大的 UIView?
我有这个巨大的 UIView
,其中有大约 600 个 UIButtons
作为子视图。 UIView
大约为 2000 x 2000 像素,它本身是 UIScrollView
的子视图。我正在尝试实现 CATiledLayer 作为这个巨大的 UIView 的渲染层,但我似乎不知道如何渲染图块。我发现了很多使用平铺图像、pdf 等覆盖 CATiledLayer
的示例,但我从未找到有关如何绘制具有大量子视图的完整 UIView 的真实示例。您可能会问为什么我想坚持使用 UIView?因为我希望用户继续使用按钮与视图交互。
我想知道是否有人有关于如何实现 - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
方法的示例或伪代码,请记住有 1巨大的 UIView
,有很多 UIbuttons
作为其子视图。
I've got this giant UIView
with around 600 UIButtons
as subviews. The UIView
is about 2000 by 2000 pixels and it's a subview on itself for a UIScrollView
. I'm trying to implement CATiledLayer
as the rendering layer for this giant UIView
but I can't seem to figure out how to render the tiles. I found a lot of examples that cover CATiledLayer
with tiled images, pdf's, ... but I never found a real example on how to draw a complete UIView with a lot of subviews. You're probably asking why i'd like to stick with UIView
? Because I'd like the users to keep on using the buttons to interact with the view.
I'm wondering if someone has an example or some psuedo code on how to implement the - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
method, keeping in mind that there's 1 giant UIView
with a lot of UIbuttons
as its subviews.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 iOS 4.0 开始,您可以使用
drawRect:
代替drawLayer:inContext:
。 Apple 的问答对此进行了解释: http://developer.apple .com/library/ios/#qa/qa1637/_index.html。其中重要的一点是,4 中的 drawRect: 现在是线程安全的,您可以使用 UIKit 图形功能。您仍然需要重写此方法才能使用
CATileLayer
:但您现在可以只使用
drawRect:
传递给该方法的矩形将是您的图块的大小,并且您需要确定需要在特定矩形中绘制什么。
CATiledLayer
的强大之处在于它只调用绘制屏幕上显示的图块。它还使用后台线程并且速度非常快。我没有将 CATiledLayer 与 UIView 一起使用,仅使用大图像。
UIViews
子类是否有自己的drawRect:
实现?我认为您需要确定哪些视图将在当前矩形中显示并调用它们的drawRect:
方法。使用
drawRect:
仅适用于 iOS 4.0 及更高版本,它不适用于旧版本。Starting with iOS 4.0, you can use
drawRect:
instead ofdrawLayer:inContext:
. This Q&A from Apple explains it: http://developer.apple.com/library/ios/#qa/qa1637/_index.html. The important point from it is that drawRect: in 4 is now thread-safe and you can use UIKit graphics functionality.You still need to override this method to use a
CATileLayer
:But you can now just use
drawRect:
The rect that is delivered to the method will be the size of your tiles and you need to determine what needs to be drawn in the particular rect. The power of
CATiledLayer
is that it only calls for drawing the tiles that are showing on the screen. It also uses background threads and is very fast.I have not used
CATiledLayer
with UIViews, only large images. Are theUIViews
subclasses with their owndrawRect:
implementations? I think you will need to determine which views will be showing in the current rect and call theirdrawRect:
method.Using
drawRect:
is only for iOS 4.0 and later, it won't work for older versions.