CALayer drawLayer:inContext: - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: CALayer drawLayer:inContext: (/thread-1368.html)
CALayer drawLayer:inContext: - dcavanagh - Apr 30, 2009 10:57 AM
I have been trying to wrap my head around this for several days, with lots of Googling and forum searching. Perhaps someone can point me in the right direction.
Basically, I am trying to flash a filled rectangle on the iPhone screen, at coordinates which I will provide. The best way I can come up with to do this is by using a CALayer, modifying its position and bounds, and animating its opacity property. However, I can't seem to wrap my head around it. The Apple docs are not in the least bit helpful, especially when it regards providing custom content to a CALayer instance.
Here is what I have done so far, in a separate test project, to try to completely isolate this problem ("layer" is a class instance variable):
Here's the initWithCoder method:
Documentation I read says that the delegate needs to implement drawLayer:inContext, so I have an additional method in my view with the layer in it:
Here's my drawRect: method in the view, which should produce a red background:
However, the layer only draws in the upper-left corner of the screen (i.e. the origin is 0,0) and the view's drawRect method doesn't get called. I would think this code should draw a 50.0x50.0 rectangle with origin 50, 50, and if I uncomment the layer.hidden=TRUE line it shouldn't display at all. I think I can figure out the animation part once I can simply get the layer to draw itself in the right spot, and for the view to draw itself properly. From what I can tell, I shouldn't need to subclass CALayer to make this work.
Could someone point me in the right direction? Thanks!
[EDIT]: Also, if this is way too complicated for what I'm trying to do, and someone has a better idea, I'm certainly open to suggestions.
CALayer drawLayer:inContext: - dcavanagh - Apr 30, 2009 03:55 PM
So, I found a hint here, and after a while, I was able to make it work. It turns out the culprit was that you are not supposed to have the view that contains the layer also serve as the layer's delegate, which happens by default for the UIView's built-in layer. I created a delegate helper object which gets instantiated in the view's setupLayers method.
Here's the code that works, for future CALayer people.
and the delegate object contains one method only:
It's too bad Apple doesn't have any sample code like this on their site - once you realize it's bad to have the view be the delegate of its own layer, which it automatically is, it's pretty straightforward.
CALayer drawLayer:inContext: - dcavanagh - May 1, 2009 10:07 AM
One last code fragment, which I hope will help others as they try to do this. Here's an example of the method that flashes the layer. Whenever the method is called, the animation will happen automagically.