CATiledLayer 用按钮绘制巨大的 UIView?

发布于 2024-11-04 02:22:02 字数 515 浏览 1 评论 0原文

我有这个巨大的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2024-11-11 02:22:02

从 iOS 4.0 开始,您可以使用 drawRect: 代替 drawLayer:inContext:。 Apple 的问答对此进行了解释: http://developer.apple .com/library/ios/#qa/qa1637/_index.html。其中重要的一点是,4 中的 drawRect: 现在是线程安全的,您可以使用 UIKit 图形功能。

您仍然需要重写此方法才能使用 CATileLayer

+(Class)layerClass
{
    return [CATiledLayer class];
}

但您现在可以只使用 drawRect:

-(void)drawRect:(CGRect)rect
{
    //normal drawing routines here.
}

传递给该方法的矩形将是您的图块的大小,并且您需要确定需要在特定矩形中绘制什么。 CATiledLayer 的强大之处在于它只调用绘制屏幕上显示的图块。它还使用后台线程并且速度非常快。

我没有将 CATiledLayer 与 UIView 一起使用,仅使用大图像。 UIViews 子类是否有自己的 drawRect: 实现?我认为您需要确定哪些视图将在当前矩形中显示并调用它们的 drawRect: 方法。

使用 drawRect: 仅适用于 iOS 4.0 及更高版本,它不适用于旧版本。

Starting with iOS 4.0, you can use drawRect: instead of drawLayer: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:

+(Class)layerClass
{
    return [CATiledLayer class];
}

But you can now just use drawRect:

-(void)drawRect:(CGRect)rect
{
    //normal drawing routines here.
}

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 the UIViews subclasses with their own drawRect: implementations? I think you will need to determine which views will be showing in the current rect and call their drawRect: method.

Using drawRect: is only for iOS 4.0 and later, it won't work for older versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文