1

I am subclassing CALayer and doing custom drawing in drawInContext: method.

The problem is I need to manually call setNeedsDisplay: after I add an instance of my subclass to a superlayer, something like:

MyCustomLayer *instance = [MyCustomLayer layer];
//do some configurations
[self.view.layer addSublayer:instance];
[layer setNeedsDisplay]; //required

or the drawing will not happen.

But when I use buit-in CALayer subclasses (CATextLayer, CAShapeLayer etc.), the call to setNeedsDisplay: is not needed after adding them to a super layer. Can I make my subclass behaves like those classes?

Eddie Deng
  • 1,399
  • 1
  • 18
  • 30

1 Answers1

1

You can call displayIfNeeded on yourself within the init method, however better is to subclass needsDisplayForKey: and return YES for the @"transform" key, which would redraw when the bounds changes (i.e. when first added to the view).

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Than you for your reply. I ended up using needsDisplayForKey: and checking for both "transform" and "bounds" keys. For me, calling displayIfNeeded or checking only "transform" key is not working for some edge cases. – Eddie Deng Nov 15 '15 at 00:53