iOS粒子效果(雪花,煙花)

雪花

//雪花動畫
- (void)animation1 {

    //粒子發(fā)射器
    CAEmitterLayer *snowEmitter = [CAEmitterLayer layer];
    //粒子發(fā)射的位置
    snowEmitter.emitterPosition = CGPointMake(100, 30);
    //發(fā)射源的大小
    snowEmitter.emitterSize     = CGSizeMake(self.view.bounds.size.width, 0.0);;
    //發(fā)射模式
    snowEmitter.emitterMode     = kCAEmitterLayerOutline;
    //發(fā)射源的形狀
    snowEmitter.emitterShape    = kCAEmitterLayerLine;
    
    //創(chuàng)建雪花粒子
    CAEmitterCell *snowflake = [CAEmitterCell emitterCell];
    //粒子的名稱
    snowflake.name = @"snow";
    //粒子參數(shù)的速度乘數(shù)因子。越大出現(xiàn)的越快
    snowflake.birthRate     = 1.0;
    //存活時間
    snowflake.lifetime      = 120.0;
    //粒子速度
    snowflake.velocity      = -10;              // falling down slowly
    //粒子速度范圍
    snowflake.velocityRange = 10;
    //粒子y方向的加速度分量
    snowflake.yAcceleration = 2;
      //周圍發(fā)射角度
    snowflake.emissionRange = 0.5 * M_PI;       // some variation in angle
    //子旋轉(zhuǎn)角度范圍
    snowflake.spinRange     = 0.25 * M_PI;      // slow spin
    //粒子圖片
    snowflake.contents      = (id) [[UIImage imageNamed:@"DazFlake"] CGImage];
    //粒子顏色
    snowflake.color         = [[UIColor redColor] CGColor];
    
    //設(shè)置陰影
    snowEmitter.shadowOpacity = 1.0;
    snowEmitter.shadowRadius  = 0.0;
    snowEmitter.shadowOffset  = CGSizeMake(0.0, 1.0);
    snowEmitter.shadowColor   = [[UIColor whiteColor] CGColor];
    
    // 將粒子添加到粒子發(fā)射器上
    snowEmitter.emitterCells = [NSArray arrayWithObject:snowflake];
    [self.view.layer insertSublayer:snowEmitter atIndex:0];
}

煙花

//煙花動畫
- (void)animation2 {
    // 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(viewBounds.size.width/2.0, 0.0);
    fireworksEmitter.emitterMode    = kCAEmitterLayerOutline;
    fireworksEmitter.emitterShape   = kCAEmitterLayerLine;
    fireworksEmitter.renderMode     = kCAEmitterLayerAdditive;
    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:@"DazRing"] 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:@"DazStarOutline"] 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種粒子組合,可以根據(jù)順序,依次煙花彈-煙花彈粒子爆炸-爆炸散開粒子
    fireworksEmitter.emitterCells   = [NSArray arrayWithObject:rocket];
    rocket.emitterCells             = [NSArray arrayWithObject:burst];
    burst.emitterCells              = [NSArray arrayWithObject:spark];
    [self.view.layer addSublayer:fireworksEmitter];
}

下載鏈接
https://pan.baidu.com/s/1dDPoeVR

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容