我设置了一个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。当我删除这一行代码时,问题依然存在。
发布于 2013-08-18 13:55:19
假设此代码位于自定义UIView中,如果是,请确保您已重写:
+ (Class)layerClass所以应该是:
+ (Class)layerClass
{
return [CAEmitterLayer class];
}这将确保在您的UIView中有
emitter = (CAEmitterLayer*)self.layer;它返回的是一个CAEmitterLayer,而不是默认的普通CALayer。
我猜这就是问题所在,因为错误表明您正在尝试调用一个只存在于普通CAEmitterLayer上的方法。
https://stackoverflow.com/questions/18299744
复制相似问题