https://github.com/imwcl/WclEmitterButton
.h
//两种不同的CAEmitterLayer
@property (strong, nonatomic) CAEmitterLayer *explosionLayer;
/**
* 开始喷射
*/
- (void)startAnimate;
/**
* 停止喷射
*/
- (void)stop;
- (void)destroy;
.m
/*
CAKeyframeAnimation // 核心动画-关键帧动画
CAEmitterLayer // 粒子发射器(其实就是一个Layer,其父类是CALayer)
CAEmitterCell // 粒子
PS:核心动画应该不用多说了;
CAEmitterLayer和CAEmitterCell,其实可以比喻成“炮”和“炮弹”,应该不难理解;
*/
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
/**
* 配置WclEmitterButton
*/
- (void)setup {
//参数详情请见博客详解:http://blog.csdn.net/wang631106979/article/details/51258020
CAEmitterCell *explosionCell = [CAEmitterCell emitterCell];
explosionCell.name = @"explosion";
explosionCell.alphaRange = 0.50;// 一个粒子的颜色alpha能改变的范围
explosionCell.alphaSpeed = -0.7;// 粒子透明度在生命周期内的改变速度
explosionCell.lifetime = 5;// 生命周期
explosionCell.lifetimeRange = 1;// 生命周期范围 lifetime= lifetime(+/-) lifetimeRange
explosionCell.birthRate = 0;// 每秒发射的粒子数量
explosionCell.velocity = 10.00;// 速度
explosionCell.velocityRange = 1500.00;// 速度范围
explosionCell.scale = 0.8;// 缩放比例
explosionCell.scaleRange = 0.4;// 缩放比例范围
explosionCell.contents = (id)[UIImage imageNamed:@"scene_finish_fly_star.png"].CGImage;// 是个CGImageRef的对象,既粒子要展现的图片
_explosionLayer = [CAEmitterLayer layer];
_explosionLayer.name = @"emitterLayer";
_explosionLayer.emitterShape = kCAEmitterLayerCircle;// 发射源的形状
_explosionLayer.emitterMode = kCAEmitterLayerOutline;// 渲染模式
_explosionLayer.emitterSize = CGSizeMake(30, 0);// 发射源的大小
_explosionLayer.emitterCells = @[explosionCell];// 粒子发射的粒子的数组
_explosionLayer.renderMode = kCAEmitterLayerOldestFirst;// 渲染模式
_explosionLayer.masksToBounds = NO;// 是否圆角
_explosionLayer.position = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);// 位置
_explosionLayer.zPosition = -1; // z轴位置
[self.layer addSublayer:_explosionLayer];
}
/**
* 开始喷射
*/
- (void)startAnimate {
//explosionLayer开始时间
self.explosionLayer.beginTime = CACurrentMediaTime();
//explosionLayer每秒喷射的个数
[self.explosionLayer setValue:@40 forKeyPath:@"emitterCells.explosion.birthRate"];
}
/**
* 停止喷射
*/
- (void)stop {
//explosionLayer每秒喷射的0个
[self.explosionLayer setValue:@0 forKeyPath:@"emitterCells.explosion.birthRate"];
}
- (void)destroy {
[self stop];
}