1. Classes used
CAEmitterLayer CAEmitterCell CAEmitterCell CAEmitterCell CAEmitterCell CAEmitterCell CAEmitterCell CAEmitterLayer and CAEmitterCell, in fact, can be compared to "guns" and "shells", should be easy to understand;Copy the code
2. There will be detailed comments in the key code directly
Required properties in 2.1. m
/** weak particle emitter */ @property (nonatomic, weak) CAEmitterLayer *emitterLayer;Copy the code
2.2 initWithFrame:
In the method
- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; If (self) {// Configure the particle emitter method [self setupEmitter]; } return self; }Copy the code
2.3 setSelected:
In the method
- (void)setSelected:(BOOL)selected { [super setSelected:selected]; // start keyframeAnimation [self keyframeAnimation]; }Copy the code
2.4 layoutSubviews
In the method
- (void)layoutSubviews{ [super layoutSubviews]; // set the particle emitter's anchor point _emitterLayer.position = self.imageview.center; }Copy the code
2.5 setupEmitter
Method (configure particle emitter method)
- (void)setup {// Initialize CAEmitterCell with CAEmitterCell *emitterCell = [CAEmitterCell emitterCell]; Emittercell. name = @"emitterCell"; Lifetime = 0.7; emitterCell.lifetime = 0.7; EmitterCell. LifetimeRange = 0.3; Emittercell.velocity = 30.00; // Emittercell.velocity = 30.00; EmitterCell. VelocityRange = 4.00; // Particle scale and scale range emittercell.scale = 0.1; EmitterCell. ScaleRange = 0.02; // Particle transparency change range emitterCell. AlphaRange = 0.10; // The rate at which particle transparency changes during its lifetime emitterCell.alphaspeed = -1.0; // Set the image emitterCell.contents = (id)[UIImage imageNamed:@"Sparkle3"].cgImage; CAEmitterLayer *layer = [CAEmitterLayer layer]; Layer. name = @"emitterLayer"; Layer. emitterShape = kCAEmitterLayerCircle; layer.emitterShape = kCAEmitterLayerCircle; Layer. emitterMode = kCAEmitterLayerOutline; // Particle launcher particles (shells to use) layer.emitterCells = @[emitterCell]; / / define particle cell can be rendered to the layer in the layer. The renderMode = kCAEmitterLayerOldestFirst; Layer. masksToBounds = NO; Layer. zPosition = -1; layer.zPosition = -1; // Add layer [self.layer addSublayer:layer]; _emitterLayer = layer; }Copy the code
Ps: It needs to be explained in detail here. CAEmitterCell attributes generally take two parameters: an average value and a “Range”, for example:
Lifetime = 0.7; emitterCell.lifetime = 0.7; EmitterCell. LifetimeRange = 0.3;Copy the code
Here’s how apple’s official documentation explains it:
Each Layer has its own random number generator, and most of the particle properties are defined as an average value and a range value, such as particle speed, whose value distribution interval is :[M-R / 2, M + R / 2]. Then in the formula M: mean value (emittercell.lifetime) R: range value (mittercell.lifetimerange) then we can calculate the range of the particle life cycle I set above according to the formula is [0.7-0.3/2, 0.7 + 0.3/2]Copy the code
2.6 keyframeAnimation
Method (Start keyframe animation)
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"]; If (self. Selected) {animation. Values = @ [@ 1.5, @ 0.8, @ 1.0, @ 1.2, @ 1.0]; Animation. Duration = 0.5; // The particle launcher fires [self startFire]; }else {animation.values = @[@0.8, @1.0]; Animation. Duration = 0.4; } / / animation model animation. CalculationMode = kCAAnimationCubic; [self.imageView.layer addAnimation:animation forKey:@"transform.scale"]; }Copy the code
This code has nothing to say and should be easy to understand.
2.7 startFire
Method (firing)
- (void) startFire {/ / second injection of 80 [self. EmitterLayer setValue: @ 1000 forKeyPath: @ "emitterCells. EmitterCell. BirthRate"]. / / self. EmitterLayer. BeginTime = CACurrentMediaTime (); // Execute stop [self performSelector:@selector(stopFire) withObject:nil afterDelay:0.1]; }Copy the code
2.8 stopFire
Method (Ceasefire)
- (void) stopFire {0 / / the number of the second injection Means that shut down [self emitterLayer setValue: @ 0 forKeyPath: @ "emitterCells. EmitterCell. BirthRate"]. }Copy the code
Finally, I’ll put two renderings
Rendering 1. PNG
Rendering 2. PNG