Amit
Amit

Reputation: 1043

handling multiple CALayer's drawing in a view

I want to implement a view having two layers on it. Then i want to do some drawing on these layers separately on press of some buttons.

I have implemented delegate method:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context

but problem is that this delegate is only fired with [myviewclass setNeedsDisplay] method(not by [mylayer setNeedsDisplay])and then drawLayer gets called for view's root layer always.

I want it to get called for mylayer with accurate context so that i can do drawing on the specific layer using its context.

Please help how to achieve this.

Upvotes: 0

Views: 1208

Answers (1)

sch
sch

Reputation: 27506

You must set the delegate of the layer and implement the delegate method drawLayer:inContext: in it. Otherwise, the layer can't determine in which object it has to call that method.

Also, the delegate of the layer can't be the UIView, so you must use something else as a delegate.

You can use the view controller for example as the delegate of your layers. First, implement the method drawLayer:inContext: in the view controller. Then, set the view controller as the delegate of your layers.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // ...
    self.view.layer1.delegate = self;
    self.view.layer2.delegate = self;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    if (layer == self.view.layer1) {
        // ...
    } else if (layer == self.view.layer2) {
        // ..
    }
}

Upvotes: 3

Related Questions