views:

264

answers:

1

I have UIView subclass - actually a puzzle piece - with 2 different CGLayer initialized at initWithFrame.

In drawRect I have to blend this two layer, and the uppermost should have variable alpha depending on game-logic.

What is the best (most performance optimized) way to do this? Is there any CGLayer, or CGContext function that sets some alpha before drawing?

+3  A: 

Set the -opacity of the layer. Remember that the layer's -opaque setting must be NO. The default is NO, but it's commonly set to YES for performance reasons.

If you're already doing -drawInContext:, then you can experiment with CGContextSetAlpha(). Generally, though, you don't use -drawRect: and layers at the same time. You usually let either the view or the layers do the drawing.

Rob Napier
You mean that I should create an independent eg. -(void)renderView method, and implement layer drawings there? Then [puzzlePiece setNeedDisplay] could be substituted by a simple [puzzlePiece renderView]?
Geri
That sounds about right, if you need to render all the pieces of the view every time. Otherwise, I would just re-render the layers you need when your model class (data) changes. There's no reason to redraw a layer just because some other layer needs to redraw.
Rob Napier
'Course.Puzzle pieces actually are allocated/addSubview-ed instances of a PuzzlePiece:UIView class, so only the recently user-manipulated piece gets the redraw message (renderView above "means" renderPuzzleView).
Geri
Thanks, bro. CGContextSetAlpha just works fine.
Geri