1

I successfully added a CAEmitter to my UIView inside a subview, but the particles emitted by that layer extend outside that view. Please see code below for the cookiecutter code and parameters. My question is how I can prevent the generated particles from exiting the parent view?

I've tried initializing the subview with a certain frame, and even resizing the bounds. In both cases, the emitted particles start inside the bounds but fly outside the view frame, seemingly without bound. They don't disappear until I limit the lifetime, or until they reach the end of the screen. Is there a way to prevent emitters from interacting with objects outside the view?

    CAEmitterLayer *emitterLayer = (CAEmitterLayer*)self.layer;

emitterLayer.name = @"emitterLayer";
emitterLayer.emitterPosition = CGPointMake(self.frame.size.width/2, 0);
emitterLayer.emitterZPosition = 0;

emitterLayer.emitterSize = CGSizeMake(1.00, 1.00);
emitterLayer.emitterDepth = 0.00;

emitterLayer.emitterShape = kCAEmitterLayerCuboid;

emitterLayer.renderMode = kCAEmitterLayerAdditive;

emitterLayer.seed = 3534563912;

// Create the emitter Cell
CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];

emitterCell.name = @"StatSap";
emitterCell.enabled = YES;

emitterCell.contents = (id)[[UIImage imageNamed:@"mysprite.png"] CGImage];
emitterCell.contentsRect = CGRectMake(0.00, 0.00, 1.00, 1.00);

emitterCell.magnificationFilter = kCAFilterTrilinear;
emitterCell.minificationFilter = kCAFilterLinear;
emitterCell.minificationFilterBias = 0.00;

emitterCell.scale = 1.00;
emitterCell.scaleRange = 0.00;
emitterCell.scaleSpeed = -0.24;

emitterCell.color = [[UIColor colorWithRed:1.00 green:0.28 blue:0.26 alpha:1.00] CGColor];
emitterCell.redRange = 1.00;
emitterCell.greenRange = 0.34;
emitterCell.blueRange = 0.31;
emitterCell.alphaRange = 0.46;

emitterCell.redSpeed = 2.03;
emitterCell.greenSpeed = 0.00;
emitterCell.blueSpeed = 0.00;
emitterCell.alphaSpeed = 0.00;

emitterCell.lifetime = 2.65;
emitterCell.lifetimeRange = 1.02;
emitterCell.birthRate = 267;
emitterCell.velocity = 155.32;
emitterCell.velocityRange = 25.00;
emitterCell.xAcceleration = 0.00;
emitterCell.yAcceleration = 911.00;
emitterCell.zAcceleration = 0.00;

// these values are in radians, in the UI they are in degrees
emitterCell.spin = -0.175;
emitterCell.spinRange = 12.566;
emitterCell.emissionLatitude = 0.000;
emitterCell.emissionLongitude = 0.000;
emitterCell.emissionRange = 6.283;

emitterLayer.emitterCells = @[emitterCell];
Erika Electra
  • 1,854
  • 3
  • 20
  • 31

1 Answers1

4

Have you made sure that the superview of the emitter has clipsToBounds = YES?

joshd
  • 1,626
  • 14
  • 17
  • joshd, thanks for the suggestion. It works if I wrap the emitter layer in its own UIView and set that UIView clipsToBounds to YES. Didn't want to go one step further to its superview because then it clips images and other subviews as well. – Erika Electra Jan 24 '14 at 08:35
  • Excellent. Yes, I meant to superview of the emitter (meaning the UIView). Thanks for the check! – joshd Jan 24 '14 at 19:43