CATiledLayer to draw giant UIView with buttons?
I've got this giant UIView
with ar开发者_如何转开发ound 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.
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.
精彩评论