美文网首页Core Animation收藏ios
IOS实现礼花粒子效果

IOS实现礼花粒子效果

作者: 口子窖 | 来源:发表于2018-09-11 19:02 被阅读65次

一、CAEmitterLayer

CAEmitterLayer是QuartzCore提供的粒子引擎, 可用于制作粒子特效。CAEmitterLayer配合CAEmitterCell使用, 它有一个属性emitterCells, 将CAEmitterCell直接添加到该数组中, 即可实现粒子效果。

二、先大致了解下CAEmitterLayer的基本属性

birthRate:粒子产生速度,默认1.0;
emitterCells: 装着CAEmitterCell对象的数组,把粒子放到layer上;
emitterDepth:决定粒子形状的深度
emitterMode:发射模式
emitterPosition:发射位置
emitterShape:发射源的形状;
emitterSize:发射源的尺寸大;
emitterZposition:发射源的z坐标位置;
lifetime:粒子生命周期
renderMode:渲染模式:
scale:粒子的缩放比例:
seed:用于初始化随机数产生的种子
spin:自旋转速度
velocity:粒子速度

三、CAEmitterCell的基本属性

birthRate:这个必须要设置,每秒产生cell数量
alphaRange:一个粒子的颜色alpha值的范围
alphaSpeed:粒子透明度在生命周期内的改变速度
blueRange:一个粒子的颜色blue 能改变的范围
blueSpeed:粒子blue在生命周期内的改变速度
color:粒子的颜色
contents:是个CGImageRef的对象,既粒子要展现的图片
emissionLatitude:发射的z轴方向的角度
emissionLongitude:x-y平面的发射方向
emissionRange:周围发射角度
emitterCells发射的粒子
enabled:粒子是否被渲染
greenrange: 一个粒子的颜色green能改变的范围
greenSpeed:粒子green在生命周期内的改变速度
lifetime:生命周期
lifetimeRange:生命周期范围
magnificationFilter:粒子过滤器放大模式
minificatonFilter:粒子过滤器缩小模式
minificationFilterBias:减小大小的因子
name:粒子的名字
redRange:一个粒子的颜色red 能改变的范围
redSpeed:粒子red在生命周期内的改变速度
scale:缩放比例
scaleRange:缩放比例范围
scaleSpeed:缩放比例速度
spin:粒子旋转角度
spinrange:粒子旋转角度范围
velocity:速度
velocityRange:速度范围
xAcceleration:粒子x方向的加速度分量
yAcceleration:粒子y方向的加速度分量
zAcceleration:粒子z方向的加速度分量

四、实现礼花🎉效果

#import "EmitterVC.h"

@interface EmitterVC ()

@end

@implementation EmitterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    UIButton * test = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    test.layer.cornerRadius = 40;
    test.center = self.view.center;
    test.backgroundColor = [UIColor purpleColor];
    [test addTarget:self action:@selector(animation2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:test];
}

- (void)animation2:(UIButton *)btn{
    //粒子发射器
    CAEmitterLayer *snowEmitter = [CAEmitterLayer layer];
    snowEmitter.emitterPosition = CGPointMake(btn.bounds.size.width/2.0, btn.bounds.size.height/2.0);
    snowEmitter.emitterSize        = btn.bounds.size;
    snowEmitter.emitterMode        = kCAEmitterLayerOutline;
    snowEmitter.emitterShape    = kCAEmitterLayerRectangle;
    snowEmitter.seed              = (arc4random() % 100) + 1;
    
    
    CAEmitterCell * spark = [CAEmitterCell emitterCell];
    spark.birthRate            = 30;
    spark.velocity            = 80;
    spark.emissionRange        = 2* M_PI;// 360 度
    spark.lifetime            = 1.0;
    spark.contents            = (id) [[UIImage imageNamed:@"emitter1"] CGImage];
    spark.scaleSpeed        = -0.8;
    spark.alphaSpeed        = -.9;
    spark.spin                = M_PI_2;
    spark.spinRange            = M_PI_2;
    
    //粒子发射器
    CAEmitterLayer *snowEmitter1 = [CAEmitterLayer layer];
    snowEmitter1.emitterPosition = CGPointMake(btn.bounds.size.width/2.0, btn.bounds.size.height/2.0);
    snowEmitter1.emitterSize        = btn.bounds.size;
    snowEmitter1.emitterMode        = kCAEmitterLayerOutline;
    snowEmitter1.emitterShape    = kCAEmitterLayerRectangle;
    snowEmitter1.seed              = (arc4random() % 100) + 1;
    
    
    CAEmitterCell * spark1 = [CAEmitterCell emitterCell];
    spark1.birthRate            = 30;
    spark1.velocity            = 100;
    spark1.emissionRange        = 2* M_PI;    // 360 度
    spark1.lifetime            = 1.0;
    spark1.contents            = (id) [[UIImage imageNamed:@"emitter2"] CGImage];
    spark1.scaleSpeed        = -0.8;
    spark1.alphaSpeed        = -.9;
    spark1.spin                = M_PI_2;
    spark1.spinRange            = M_PI_2;
    
    // 将粒子添加到粒子发射器上
    snowEmitter.emitterCells = [NSArray arrayWithObject:spark];
    snowEmitter1.emitterCells = [NSArray arrayWithObject:spark1];
    
    [btn.layer addSublayer:snowEmitter];
    [btn.layer addSublayer:snowEmitter1];
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
    
    dispatch_time_t popTime2 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
    
    __weak typeof(snowEmitter) weakselEmitter = snowEmitter;
    __weak typeof(snowEmitter1) weakselEmitter1 = snowEmitter1;
    
    dispatch_after(popTime, dispatch_get_main_queue(), ^{
        __strong typeof(weakselEmitter) strongEmitter = weakselEmitter;
        __strong typeof(weakselEmitter1) strongEmitter1 = weakselEmitter1;
        strongEmitter.birthRate = 0;
        strongEmitter1.birthRate = 0;
    });
    
    dispatch_after(popTime2, dispatch_get_main_queue(), ^{
        __strong typeof(weakselEmitter) strongEmitter = weakselEmitter;
        __strong typeof(weakselEmitter1) strongEmitter1 = weakselEmitter1;
        [strongEmitter removeFromSuperlayer];
        [strongEmitter1 removeFromSuperlayer];
    });
}

- (void)animation1{
    // Cells spawn in the bottom, moving up
    //分为3种粒子,子弹粒子,爆炸粒子,散开粒子
    CAEmitterLayer *fireworksEmitter = [CAEmitterLayer layer];
    CGRect viewBounds = self.view.layer.bounds;
    fireworksEmitter.emitterPosition = CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height);
    fireworksEmitter.emitterSize    = CGSizeMake(200, 200);
    fireworksEmitter.emitterMode    = kCAEmitterLayerPoints;
    fireworksEmitter.emitterShape    = kCAEmitterLayerPoint;
    fireworksEmitter.renderMode        = kCAEmitterLayerBackToFront;
    fireworksEmitter.seed = (arc4random()%100)+1;
    
    // Create the rocket
    CAEmitterCell* rocket = [CAEmitterCell emitterCell];
    
    rocket.birthRate        = 1.0;
    rocket.emissionRange    = 0.25 * M_PI;  // some variation in angle
    rocket.velocity            = 380;
    rocket.velocityRange    = 100;
    rocket.yAcceleration    = 75;
    rocket.lifetime            = 1.02;    // we cannot set the birthrate < 1.0 for the burst
    
    //星星图片
    rocket.contents            = (id)[[UIImage imageNamed:@"emitter1"] CGImage];
    rocket.scale            = 0.2;
    rocket.color            = [[UIColor redColor] CGColor];
    rocket.greenRange        = 1.0;        // different colors
    rocket.redRange            = 1.0;
    rocket.blueRange        = 1.0;
    rocket.spinRange        = M_PI;        // slow spin
    
    // the burst object cannot be seen, but will spawn the sparks
    // we change the color here, since the sparks inherit its value
    CAEmitterCell* burst = [CAEmitterCell emitterCell];
    
    burst.birthRate            = 1.0;        // at the end of travel
    burst.velocity            = 0;        //速度为0
    burst.scale                = 2.5;      //大小
    burst.redSpeed            =-1.5;        // shifting
    burst.blueSpeed            =+1.5;        // shifting
    burst.greenSpeed        =+1.0;        // shifting
    burst.lifetime            = 0.35;     //存在时间
    
    // and finally, the sparks
    CAEmitterCell* spark = [CAEmitterCell emitterCell];
    
    spark.birthRate            = 400;
    spark.velocity            = 125;
    spark.emissionRange        = 2* M_PI;    // 360 度
    spark.yAcceleration        = 75;        // gravity
    spark.lifetime            = 3;
    //星星图片
    spark.contents            = (id) [[UIImage imageNamed:@"emitter2"] CGImage];
    spark.scaleSpeed        =-0.2;
    spark.greenSpeed        =-0.1;
    spark.redSpeed            = 0.4;
    spark.blueSpeed            =-0.1;
    spark.alphaSpeed        =-0.25;
    spark.spin                = 2* M_PI;
    spark.spinRange            = 2* M_PI;
    
    // 3种粒子组合,可以根据顺序,依次烟花弹-烟花弹粒子爆炸-爆炸散开粒子
    fireworksEmitter.emitterCells    = [NSArray arrayWithObject:rocket];
    rocket.emitterCells                = [NSArray arrayWithObject:burst];
    burst.emitterCells                = [NSArray arrayWithObject:spark];
    [self.view.layer addSublayer:fireworksEmitter];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

四、效果图

gif5新文件(1).gif

相关文章

网友评论

    本文标题:IOS实现礼花粒子效果

    本文链接:https://www.haomeiwen.com/subject/mmxpgftx.html