Exactly what should happen in a CALayer's disp

2019-07-15 15:30发布

问题:

Which, if either, of these methods would be an appropriate place to:

  • Change the text of a CATextLayer
  • Load a different image into a CAImageLayer
  • Tell sublayers to update themselves

回答1:

Dude I may be way drunk ... but there is NO drawRect method in CAlayers

I think you can use drawInContext: to actually (gulp) draw in to CALayers, but nobody is man enough to do that since WW2.

Regarding display, you don't need to call it, it basically updates what you set using .contents.

I just use .contents something like this ...

[self.view setLayer:rearLayer];
[self.view setWantsLayer:YES];
rearLayer.frame = CGRectMake(gameBlah,gameBlah, 1024,768);
// note that we are dealing there with the mysteries of rearLayer positioning;
// it is measured by the SUPER layer of the layer in question!
// (indeed much as frame for the view is, if you think about it ..)
rearLayer.contents = (id)loadMacStylePng(@"spaceShip");

Say one had the guts to write one's own drawInContext: ...

In that case, it gets called (or abstracted out ... or recalculated, or something) when you call displayAsNeeded. (I've never needed to call displayAsNeeded:, that's for sure.)