0

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;
}
serg
  • 109,619
  • 77
  • 317
  • 330
  • Are you running any animations? Show the code for moving the emitter. – Wain Aug 02 '13 at 21:26
  • @Wain Added some code. Don't have any animations running. – serg Aug 02 '13 at 21:46
  • No animations anywhere? Maybe in a method that calls `showSparkles`? – Wain Aug 02 '13 at 21:52
  • @Wain I had one `[UIView animateWithDuration]` but I commented it out to be sure, same thing. I assume this layer emitter itself is an aways running animation, isn't it. By changing birthRate it doesn't really stop it. – serg Aug 02 '13 at 22:11
  • Changing `birthRate` can stop new 'animations' from being started. – Wain Aug 02 '13 at 22:19
  • Ok got it kind of working by removing and recreating the whole layer. – serg Aug 02 '13 at 22:26

0 Answers0