I want to display sparkles for the short period when a user touches the screen (placed at the touch position).
The problem is when I change CAEmitterLayer's emitterPosition
it acts as if it is trying to animate this position from old value to new one during about 0.5s period, instead of changing it instantly.
For example the emitter position is set to (0,0) by default, so when I try to touch at the center of the screen for the first time, it emits a line of sparkles from (0,0) to current position, instead of emitting sparkles form the center.
To stop sparkles, I set layer's birthRate
to 0 after a short delay, then on touch I change emitterPosition
and turn birthRate
back to 1.
The only solution I could find is to have 0.5s delay between moving the position and turning birthRate
on.
- (void)initSparkles {
layer = [CAEmitterLayer layer];
layer.birthRate = 0;
[sparkleView.layer addSublayer:layer];
}
- (void)showSparkles:(CGPoint)position {
layer.emitterPosition = position;
CAEmitterCell *sparkle = [CAEmitterCell emitterCell];
sparkle.props = ...;
layer.emitterCells = [NSArray arrayWithObjects:sparkle, nil];
layer.birthRate = 1.0;
[self performSelector:@selector(stopSparkles) withObject:nil afterDelay:0.05 ];
}
- (void)stopSparkles {
layer.birthRate = 0.0;
}