方法发送到CAEmitterLayer但转到CALayer

时间:2013-08-18 13:45:52

标签: iphone objective-c calayer caemitterlayer

我设置了一个CAEmitterLayer,它运行正常:

-(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];


}

但我添加了一行代码:

-(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];


}

突然之间,我得到Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayer setEmitterPosition:]: unrecognized selector sent to instance 0x7546390'。调试窗口说该对象是CAEmitterLayer,而不仅仅是CALayer。当我删除那行代码时,问题仍然存在。

1 个答案:

答案 0 :(得分:3)

我假设此代码在自定义UIView中,如果是这样,请确保已覆盖:

+ (Class)layerClass

所以它应该是:

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

这将确保你所在的UIView里面有

emitter = (CAEmitterLayer*)self.layer;

它返回一个CAEmitterLayer,而不是它默认执行的普通CALayer。

我猜这是问题所在,因为错误表明你试图在普通的CALayer上调用一个只存在于CAEmitterLayer上的方法。