3

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?

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172

1 Answers1

6

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
  • 286,113
  • 34
  • 456
  • 610
  • 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 Borbás Jan 06 '10 at 23:15
  • 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 Jan 07 '10 at 00:24
  • '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 Borbás Jan 07 '10 at 13:09
  • Hey @RobNapier, I have a image on my tableCell and on tap of the cell I am opening my drawingView and drawing the image into a Cglayer and showing it on the drawingView. Now whatever I draw should be indepenedent of that image. So that whenever I do undo and redo, that cell Image should not be affected. How can we achieve this? – Ranjit Jan 13 '14 at 14:06
  • I don't quite understand what you mean here, but it looks like a separate question rather than a comment for this question. The general answer to undo is that you need to keep track of the stack of either images or (usually better) instructions for drawing the images, and move up and down your stack. See NSUndoManager for managing that stack. – Rob Napier Jan 13 '14 at 14:10
  • @RobNapier. thanks, I will post a question for this. I have a question posted, regarding CGLayers http://stackoverflow.com/questions/21027788/doing-undo-and-redo-with-cglayer-drawing I am not getting any clue for this. It would be very helpful, if you suggest me something regarding this question. So that my car moves ahead. – Ranjit Jan 13 '14 at 15:26
  • @RobNapier, how does clearing the LayerContext, using CGContextClearRect affect the data present inside layer – Ranjit Mar 06 '14 at 07:43
  • It should clear it. Are you seeing something different? When you say the "LayerContext" I assume you mean the result of `CGLayerGetContext()` (that may not be the same context you passed to `CGLayerCreateWithContext()`). – Rob Napier Mar 06 '14 at 13:22