CAEmitterLayer(粒子系统)学习笔记(含Demo)

一、系统剖析 在UIKit中,粒子系统由两部分组成: ·一个或多个CAEmitterCells:发射器电池可以看作是单个粒子的原型(例如,一个单一的粉扑在一团烟雾)。当散发出一个粒子,UIKit根据这个发射粒子和定义的基础上创建一个随机粒子。此原型包括一些属性来控制粒子的图片,颜色,方向,运动,缩放比例和生命周期。
·一个或多个CAEmitterLayers,但通常只有一个:这个发射的层主要控制粒子的形状(例如,一个点,矩形或圆形)和发射的位置(例如,在矩形内,或边缘)。这个层具有全局的乘法器,可以施加到系统内的CAEmitterCells。这些给你一个简单的方法覆盖的所有粒子的变化。比如一个人为的例子将改变x雨来模拟风的速度。
基础是简单的,但这些参数却是相当微妙的。CAEmitterLayer有超过30种不同的参数进行自定义粒子的行为。下面,我就拼出来的一些特殊问题


二、不可测性
1、是什么让粒子系统成为一个随机的系统?
CAEmitterCell的属性一般有两个参数:一个均值和一个“cone”,比如velocity 和velocityRange。
默认情况下,这个“cone”是0,这就以为着所有粒子将具有相同的速度。通过改变这个“cone”,每个发射粒子会随机被扰动获得一个这个“cone”范围内的值。这点在Apple官方文档CAEmitterLayer documentation:有讲解:
Each layer has its ownrandom number generator state. Emitter cell properties that are defined as amean and a range, such as a cell’s speed, the value of the properties areuniformly distributed in the interval [M - R/2, M + R/2].
2、发射的方向
CAEmitterCells有一个velocity(速度)的属性,这意味着发送方向上的速度。实际上这个发射的方向是通过emissionLongitude属性定义的。Apple这样阐述的:
The emission longitude is theorientation of the emission angle in the xy-plane. it is also often referred toas the azimuth.


三、代码


- (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"; 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]; }



代码设置/结构说明:
1、CAEmitterCell

CAEmitterCell *effectCell = [CAEmitterCell emitterCell];
effectCell 几个重要属性:
1).birthRate 顾名思义没有这个也就没有effectCell,这个必须要设置,具体含义是每秒某个点产生的effectCell数量
2).lifetime & lifetimeRange 表示effectCell的生命周期,既在屏幕上的显示时间要多长。
3).contents 这个和CALayer一样,只是用来设置图片
4).name 这个是当effectCell存在caeEmitter 的emitterCells中用来辨认的。用到setValue forKeyPath比较有用
5).velocity & velocityRange & emissionRange 表示cell向屏幕右边飞行的速度 & 在右边什么范围内飞行& +-角度扩散
6).把cell做成array放进caeEmitter.emitterCells里去。caeEmitter.renderMode有个效果很不错,能变成火的就是kCAEmitterLayerAdditive
属性:
alphaRange:一个粒子的颜色alpha能改变的范围;
alphaSpeed:粒子透明度在生命周期内的改变速度;
birthrate:粒子参数的速度乘数因子;
blueRange:一个粒子的颜色blue 能改变的范围;
blueSpeed: 粒子blue在生命周期内的改变速度;
color:粒子的颜色
contents:是个CGImageRef的对象,既粒子要展现的图片;
contentsRect:应该画在contents里的子rectangle:
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:子旋转角度范围
style:不是很清楚:
velocity:速度
velocityRange:速度范围
xAcceleration:粒子x方向的加速度分量
yAcceleration:粒子y方向的加速度分量
zAcceleration:粒子z方向的加速度分量

2、CAEmitterLayer CAEmitterLayer提供了一个基于Core Animation的粒子发射系统,粒子用CAEmitterCell来初始化。粒子画在背景层盒边界上
属性:
birthRate:粒子产生系数,默认1.0;
emitterCells: 装着CAEmitterCell对象的数组,被用于把粒子投放到layer上;
emitterDepth:决定粒子形状的深度联系:emittershape
emitterMode:发射模式
NSString * const kCAEmitterLayerPoints;
NSString * const kCAEmitterLayerOutline;
NSString * const kCAEmitterLayerSurface;
NSString * const kCAEmitterLayerVolume;


emitterPosition:发射位置
emitterShape:发射源的形状:
NSString * const kCAEmitterLayerPoint;
NSString * const kCAEmitterLayerLine;
NSString * const kCAEmitterLayerRectangle;
NSString * const kCAEmitterLayerCuboid;
NSString * const kCAEmitterLayerCircle;
NSString * const kCAEmitterLayerSphere;


emitterSize:发射源的尺寸大;
emitterZposition:发射源的z坐标位置;
lifetime:粒子生命周期
preservesDepth:不是多很清楚(粒子是平展在层上)
renderMode:渲染模式:
NSString * const kCAEmitterLayerUnordered;
NSString * const kCAEmitterLayerOldestFirst;
NSString * const kCAEmitterLayerOldestLast;
NSString * const kCAEmitterLayerBackToFront;
NSString * const kCAEmitterLayerAdditive;

scale:粒子的缩放比例:
seed:用于初始化随机数产生的种子
spin:自旋转速度
【CAEmitterLayer(粒子系统)学习笔记(含Demo)】velocity:粒子速度


四、Demo下载



    推荐阅读