Question

I set up a CAEmitterLayer and it works perfectly:

-(void)awakeFromNib
{
    //set ref to the layer
    emitter = (CAEmitterLayer*)self.layer;
    emitter.emitterPosition = CGPointMake(160, 270);

    CAEmitterCell* grassLeft = [self getEmitter];
    CAEmitterCell* grassRight = [self getEmitter];
    grassLeft.emissionLongitude = M_PI*1.20;
    grassRight.emissionLongitude = M_PI*1.80;

    //add the cell to the layer and we're done
    emitter.emitterCells = [NSArray arrayWithObjects:grassLeft,grassRight,nil];


}

But I added one line of code:

-(void)awakeFromNib {
    //set ref to the layer
    emitter = (CAEmitterLayer*)self.layer;
    emitter.emitterPosition = CGPointMake(160, 270);
    emitter.backgroundColor = [[UIColor redColor] CGColor];

    CAEmitterCell* grassLeft = [self getEmitter];
    CAEmitterCell* grassRight = [self getEmitter];
    grassLeft.emissionLongitude = M_PI*1.20;
    grassRight.emissionLongitude = M_PI*1.80;

    //add the cell to the layer and we're done
    emitter.emitterCells = [NSArray arrayWithObjects:grassLeft,grassRight,nil];


}

And all of a sudden, I get Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayer setEmitterPosition:]: unrecognized selector sent to instance 0x7546390'. The debugging window says that object is a CAEmitterLayer, not just a CALayer. And when I delete that line of code, the problem persists.

Was it helpful?

Solution

I'm assuming this code is within a custom UIView, if so, make sure that you have overridden:

+ (Class)layerClass

So it should be:

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

This will ensure that inside your UIView where you have

emitter = (CAEmitterLayer*)self.layer;

It is returning a CAEmitterLayer, rather than a normal CALayer which it does by default.

I'm guessing this is the issue since the error is stating you are trying to call a method that only exists on CAEmitterLayer on a normal CALayer.

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