1. CAEmitterLayer
CAEmitterLayer 是一个高性能的粒子引擎,被用来创建复杂的粒子发射动画,而所谓的粒子就是CAEmitterCell 。可以把CAEmitterLayer 看做CAEmitterCell 的容器,CAEmitterCell 可以进行个性化定制。
CAEmitterLayer 主要属性
@property(nullable, copy) NSArray<CAEmitterCell *> *emitterCells;
@property CGPoint emitterPosition;
@property CGFloat emitterZPosition;
@property CGSize emitterSize;
@property CGFloat emitterDepth;
@property(copy) CAEmitterLayerEmitterShape emitterShape;
@property(copy) CAEmitterLayerEmitterMode emitterMode;
@property(copy) CAEmitterLayerRenderMode renderMode;
@property float birthRate;
@property float lifetime;
@property float velocity;
@property float scale;
@property float spin;
emitterShape 和emitterMode 需要配合使用,我们设置CAEmitterCell 的velocity 为正数
发射源的形状 | 说明 |
---|
kCAEmitterLayerPoint | 从点往右发射粒子 | kCAEmitterLayerLine | emitterMode是kCAEmitterLayerPoints时,从两个端点往右发射粒子, 其他都是往上发射粒子 | kCAEmitterLayerRectangle kCAEmitterLayerCuboid | emitterMode是kCAEmitterLayerOutline时,往四周发射粒子, emitterMode是kCAEmitterLayerPoints时,从四个端点往右发射粒子, 其他都是往右发射粒子 | kCAEmitterLayerCircle kCAEmitterLayerSphere | emitterMode是kCAEmitterLayerOutline时,往四周发射粒子, emitterMode是kCAEmitterLayerPoints时,从中心点往右发射粒子, 其他都是往右发射粒子 |
CAEmitterCell 的velocity 为负数时,方向与正数时相反。
2. CAEmitterCell
CAEmitterCell 实现了粒子效果,定义所需粒子样式。
@property(nullable, copy) NSString *name;
@property(getter=isEnabled) BOOL enabled;
@property float birthRate;
@property float lifetime;
@property float lifetimeRange;
@property CGFloat velocity;
@property CGFloat velocityRange;
@property CGFloat xAcceleration;
@property CGFloat yAcceleration;
@property CGFloat zAcceleration;
@property CGFloat scale;
@property CGFloat scaleRange;
@property CGFloat scaleSpeed;
@property CGFloat spin;
@property CGFloat spinRange;
@property(nullable) CGColorRef color;
@property float redRange;
@property float greenRange;
@property float blueRange;
@property float alphaRange;
@property float redSpeed;
@property float greenSpeed;
@property float blueSpeed;
@property float alphaSpeed;
@property CGFloat emissionLatitude;
@property CGFloat emissionLongitude;
@property CGFloat emissionRange;
@property(nullable, strong) id contents;
@property(nullable, copy) NSArray<CAEmitterCell *> *emitterCells;
3. 效果示例
3.1 下雪效果
示例代码
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
emitterLayer.frame = self.view.bounds;
emitterLayer.emitterPosition = CGPointMake(self.view.center.x, 100);
emitterLayer.emitterSize = CGSizeMake(self.view.bounds.size.width-50, 10);
emitterLayer.emitterShape = kCAEmitterLayerLine;
emitterLayer.emitterMode = kCAEmitterLayerSurface;
CAEmitterCell *cell = [CAEmitterCell emitterCell];
cell.contents = (id)[UIImage imageNamed:@"launch"].CGImage;
cell.birthRate = 10;
cell.lifetime = 10;
cell.lifetimeRange = 2;
cell.scale = 0.2;
cell.scaleRange = 0.05;
cell.yAcceleration = 20;
cell.alphaSpeed = -0.1;
emitterLayer.emitterCells = @[cell];
效果如下
3.2 爆炸效果
示例代码
- (void)onExplodeClick:(UIButton *)sender {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.repeatCount = 1;
animation.duration = 0.2;
animation.toValue = @1.2;
[sender.layer addAnimation:animation forKey:nil];
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
emitterLayer.beginTime = CACurrentMediaTime();
emitterLayer.frame = self.view.bounds;
emitterLayer.emitterPosition = sender.center;
emitterLayer.emitterSize = CGSizeMake(50, 50);
emitterLayer.emitterShape = kCAEmitterLayerCircle;
emitterLayer.emitterMode = kCAEmitterLayerOutline;
CAEmitterCell *cell = [CAEmitterCell emitterCell];
cell.contents = (id)[UIImage imageNamed:@"launch"].CGImage;
cell.birthRate = 2000;
cell.lifetime = 0.3;
cell.lifetimeRange = 0.05;
cell.scale = 0.03;
cell.scaleRange = 0.01;
cell.velocity = 80;
cell.velocityRange = 20;
emitterLayer.emitterCells = @[cell];
[self.view.layer addSublayer:emitterLayer];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
emitterLayer.birthRate = 0;
});
}
效果如下
参考资料: iOS进阶之CAEmitterLayer 参考资料: 关于CAEmitterLayer和CAEmitterCell结合使用
源码下载: https://github.com/nai-chen/IosBlog
|