首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

iOS CAEmitterCell 與 CAEmitterLayer 詳解:粒子系統開發指南

     編輯:關於C++

粒子系統組成

UIKit 粒子系統由兩部分組成:

  1. CAEmitterCell:單個粒子原型,定義圖片、顏色、方向、運動、縮放、生命周期等屬性。一個發射層可包含多個 Cell。
  2. CAEmitterLayer:發射器層,繼承自 CALayer,控制發射形狀(點、線、矩形等)、位置、大小、模式(點/輪廓/表面/體積)、全局乘數(影響所有 Cell)。

核心屬性

CAEmitterLayer

  • emitterPosition:發射中心點。
  • emitterSize:發射區域大小。
  • emitterShape:kCAEmitterLayerPoint/Line/Rectangle/Cuboid/Circle/Sphere。
  • emitterMode:kCAEmitterLayerPoints/Outline/Surface/Volume。
  • renderMode:渲染順序(無序/最舊優先/最新優先/後向前/加法混合)。
  • birthRate、lifetime、velocity、scale、spin 等全局乘數。

CAEmitterCell

  • contents (CGImageRef):粒子圖片。
  • color (CGColorRef):粒子顏色。
  • birthRate、lifetime、lifetimeRange:產生率與壽命。
  • velocity、velocityRange、xAcceleration/yAcceleration/zAcceleration:運動參數。
  • emissionLongitude、emissionLatitude、emissionRange:發射方向與角度範圍。
  • spin、spinRange:自旋轉。
  • scale、scaleRange、scaleSpeed:縮放動畫。
  • alphaSpeed、redSpeed 等:顏色隨時間變化速度。

代碼示例:雪花+星星混合下落

CAEmitterLayer *snowEmitter = [CAEmitterLayer layer];
snowEmitter.emitterPosition = CGPointMake(120, 20);
snowEmitter.emitterSize = CGSizeMake(self.view.bounds.size.width * 20, 20);
snowEmitter.emitterMode = kCAEmitterLayerSurface;
snowEmitter.emitterShape = kCAEmitterLayerLine;

CAEmitterCell *snowflake = [CAEmitterCell emitterCell];
snowflake.name = @"snow";
snowflake.birthRate = 1.0; snowflake.lifetime = 120.0;
snowflake.velocity = 10.0; snowflake.velocityRange = 10;
snowflake.yAcceleration = 2;
snowflake.emissionRange = 0.5 * M_PI;
snowflake.spinRange = 0.25 * M_PI;
snowflake.contents = (id)[[UIImage imageNamed:@"DazFlake"] CGImage];
snowflake.color = [[UIColor colorWithRed:0.2 green:0.258 blue:0.543 alpha:1] CGColor];

CAEmitterCell *star = [CAEmitterCell emitterCell];
star.name = @"star";
star.birthRate = 1.0; star.lifetime = 120.0;
star.velocity = 10.0; star.velocityRange = 10;
star.yAcceleration = 2;
star.emissionRange = 0.5 * M_PI;
star.spinRange = 0.25 * M_PI;
star.contents = (id)[[UIImage imageNamed:@"DazStarOutline"] CGImage];
star.color = [[UIColor colorWithRed:0.6 green:0.658 blue:0.743 alpha:1] CGColor];

snowEmitter.emitterCells = @[snowflake, star];
snowEmitter.shadowOpacity = 1.0; snowEmitter.shadowRadius = 0;
snowEmitter.shadowOffset = CGSizeMake(0, 1);
snowEmitter.shadowColor = [[UIColor redColor] CGColor];
[self.view.layer insertSublayer:snowEmitter atIndex:0];

效果

雪花與星星粒子從頂部線狀區域發射,受重力加速下落,帶旋轉、陰影,視覺效果豐富。

Particle effect screenshot

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved