在 UIKit 中,粒子系統由兩部分組成。
CAEmitterCell:可視為單一粒子的原型。比如一團煙霧裡的一顆粉粒。系統每次要發射粒子時,會依這個原型加上隨機擾動,生成一個個略有差異的個體。原型上定義了圖片、顏色、方向、運動、縮放與生命週期等屬性。
CAEmitterLayer:通常只有一個。它負責控制粒子的發射形狀(點、矩形、圓形等)與發射位置(矩形內部、邊緣等),同時提供一組全域乘數,可以統一覆寫底下所有 cell 的變化。舉例來說,想讓整片雨絲被風吹斜,改這一層就够了。
結構簡單,參數卻很微妙——CAEmitterLayer 有三十多個可調項目。以下挑幾個容易搞錯的說明。
CAEmitterCell 的屬性大多是成對出現的:一個平均值加一個範圍,例如 velocity 與 velocityRange。預設的範圍是 0,代表所有粒子完全一致。把範圍拉開之後,每顆粒子就會在這個區間內被隨機取值。Apple 的說法是:
Each layer has its own random number generator state. Emitter cell properties that are defined as a mean and a range, such as a cell's speed, the value of the properties are uniformly distributed in the interval [M - R/2, M + R/2].
也就是說,實際值均勻分布在中值 ± 範圍/2 之間。
CAEmitterCell 的 velocity 只給出「速度大小」,方向其實由 emissionLongitude 定義:
The emission longitude is the orientation of the emission angle in the xy-plane. It is also often referred to as the azimuth.
換句話說,想改粒子往哪飛,該動的是 emissionLongitude,而不是 velocity。
下面的例子讓粒子持續發射,並用 KVC 動態改變發射角度,做出一個可調方向的噴射效果。
- (void)viewDidLoad {
[super viewDidLoad];
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
emitterLayer.emitterPosition = self.view.center;
_emitterLayer = emitterLayer;
[self.view.layer addSublayer:emitterLayer];
CAEmitterCell *funnyEmitterCell = [CAEmitterCell emitterCell];
funnyEmitterCell.contents = (id)[UIImage imageNamed:@"funny.jpg"].CGImage;
funnyEmitterCell.birthRate = 10.0;
funnyEmitterCell.velocity = 200.0;
funnyEmitterCell.lifetime = 5.0;
funnyEmitterCell.scale = 0.1;
funnyEmitterCell.name = @"funny"; // 供 KVC 定位使用
emitterLayer.emitterCells = [NSArray arrayWithObject:funnyEmitterCell];
[self bumpAngle];
UILabel *angleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];
angleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:angleLabel];
_angleLabel = angleLabel;
}
- (void)bumpAngle {
NSNumber *emissionLongitude =
[_emitterLayer valueForKeyPath:@"emitterCells.funny.emissionLongitude"];
NSNumber *nextLongitude =
[NSNumber numberWithFloat:[emissionLongitude floatValue] + 0.02];
[_emitterLayer setValue:nextLongitude
forKeyPath:@"emitterCells.funny.emissionLongitude"];
_angleLabel.text = [NSString stringWithFormat:@"%.0f degrees",
[nextLongitude floatValue] * 180 / M_PI];
[self performSelector:@selector(bumpAngle) withObject:nil afterDelay:0.1];
}
這裡的關鍵是 cell 的 name 屬性:只有設了名字,才能用 emitterCells.funny.xxx 這樣的 key path 精準打到某一顆 cell 去改參數。
birthRate:沒它就沒有粒子,必須設定,代表每秒產生的數量
lifetime 與 lifetimeRange:粒子在螢幕上停留多久
contents:設定粒子圖片,用法與 CALayer 一致
name:粒子名稱,搭配 setValue:forKeyPath: 使用
velocity、velocityRange、emissionRange:飛行速度、速度範圍與角度擴散
把 cell 放進陣列交給 emitterLayer.emitterCells 即可。另外 renderMode 設為 kCAEmitterLayerAdditive 時,粒子堆疊會產生火焰般的加色效果,很值得一試。
birthRate:粒子產生係數,預設 1.0
emitterCells:裝 CAEmitterCell 的陣列
emitterDepth:決定粒子形狀的深度關聯,與 emitterShape 搭配
emitterMode:發射模式,可選 kCAEmitterLayerPoints、Outline、Surface、Volume
emitterPosition:發射位置
emitterShape:發射源形狀,可選 Point、Line、Rectangle、Cuboid、Circle、Sphere
emitterSize:發射源尺寸
emitterZposition:發射源 z 座標
lifetime:粒子生命週期
preservesDepth:是否保留深度
renderMode:渲染模式,可選 Unordered、OldestFirst、OldestLast、BackToFront、Additive
scale:粒子縮放比例
seed:隨機數種子
spin:自旋速度
velocity:粒子速度