一、系统剖析
在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];
}










