0

我设置了一个 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。当我删除那行代码时,问题仍然存在。

4

1 回答 1

3

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

+ (Class)layerClass

所以应该是:

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

这将确保在您拥有的 UIView 内部

emitter = (CAEmitterLayer*)self.layer;

它返回一个 CAEmitterLayer,而不是默认情况下的普通 CALayer。

我猜这是问题,因为错误表明您正在尝试调用仅存在于普通 CALayer 上的 CAEmitterLayer 上的方法。

于 2013-08-18T13:55:19.710 回答