UIKit 粒子系統由兩部分組成:
emitterPosition:發射中心點。emitterSize:發射區域大小。emitterShape:kCAEmitterLayerPoint/Line/Rectangle/Cuboid/Circle/Sphere。emitterMode:kCAEmitterLayerPoints/Outline/Surface/Volume。renderMode:渲染順序(無序/最舊優先/最新優先/後向前/加法混合)。birthRate、lifetime、velocity、scale、spin 等全局乘數。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];
雪花與星星粒子從頂部線狀區域發射,受重力加速下落,帶旋轉、陰影,視覺效果豐富。
