Question

I am trying to follow the tutorial about iOS particle systems here: http://www.raywenderlich.com/6063/uikit-particle-systems-in-ios-5-tutorial

I am having trouble casting the self.canvas.layer in C4Workspace.m to a CAEmitterLayer. The code compiles just fine but fails at runtime.

I tried this:

    particlesystem = (CAEmitterLayer *)self.canvas.layer;

But I receive this error every time.

-[C4Layer setEmitterPosition:]: unrecognized selector sent to instance 0xa183830

It seems that I am not casting or exposing methods properly. How might I do this?

Was it helpful?

Solution

You cannot simply cast one layer as another. In order for a view to have a non-standard layer, you need to subclass it and define the +layerClass method:

@implementation MyViewSubclass

+ (Class)layerClass {
    return [CAEmitterLayer class];
}

...

Unfortunately for your case, the view you're working with has already set up a custom layer, C4Layer, which can be seen on GitHub. This layer is doing a lot and you don't want to try replacing it.

What you can do is insert your own sublayer into your canvas:

CAEmitterLayer *myLayer = [CAEmitterLayer layer];
myLayer.frame = self.canvas.bounds;
[self.canvas.layer addSublayer:myLayer];

This emitter layer will now overlay your layer and you can add any effects you want. If you want the emitter below other layers, you can use insertSublayer:myLayer atIndex:0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top