1.CAEmitterLayer (粒子发射器)
CAEmitterLayer
继承于 : CALayer
属性介绍:
// 用来存储不同的粒子
NSArray<CAEmitterCell *> *emitterCells;
//粒子的单次发射量;
@property float birthRate;
//粒子的生命周期;
@property float lifetime;
//发射粒子的中心点
@property CGPoint emitterPosition;
//如果是立体效果的时候需要用到这个值; 默认值:(x:0,y:0,z:0)
@property CGFloat emitterZPosition;
//发射源的size
@property CGSize emitterSize;
@property CGFloat emitterDepth; //深度
//形状 ,其实是一个String类型:
//typedef NSString * CAEmitterLayerEmitterShape NS_TYPED_ENUM;
//typedef NSString * CAEmitterLayerEmitterMode NS_TYPED_ENUM;
//typedef NSString * CAEmitterLayerRenderMode NS_TYPED_ENUM;
/*定义所用发射形状类型的字符串。当前的选项是:
kCAEmitterLayerPoint : 点
kCAEmitterLayerLine : 线
kCAEmitterLayerRectangle : 矩形
kCAEmitterLayerCuboid : 立体矩形
kCAEmitterLayerCircle : 圆
kCAEmitterLayerSphere : 球
*/
@property(copy) CAEmitterLayerEmitterShape emitterShape;
/* A string defining how particles are created relative to the emission
* shape. Current options are `points', `outline', `surface' and
* `volume' (the default). */
@property(copy) CAEmitterLayerEmitterMode emitterMode;
/* A string defining how particles are composited into the layer's
* image. Current options are `unordered' (the default), `oldestFirst',
* `oldestLast', `backToFront' (i.e. sorted into Z order) and
* `additive'. The first four use source-over compositing, the last
* uses additive compositing. */
//渲染模式
@property(copy) CAEmitterLayerRenderMode renderMode;
/* When true the particles are rendered as if they directly inhabit the
* three dimensional coordinate space of the layer's superlayer, rather
* than being flattened into the layer's plane first. Defaults to NO.
* If true, the effect of the `filters', `backgroundFilters' and shadow-
* related properties of the layer is undefined. */
//是否需要深度
@property BOOL preservesDepth;
/* Multiplies the cell-defined particle velocity. Defaults to one.
* Animatable. */
//粒子运动速度 默认是1.0 需要加速,则增加
@property float velocity;
/* Multiplies the cell-defined particle scale. Defaults to one. Animatable. */
//缩放比例
@property float scale;
/* Multiplies the cell-defined particle spin. Defaults to one. Animatable. */
//自旋速度
@property float spin;
/* The seed used to initialize the random number generator. Defaults to
zero. Each layer has its own RNG state. For properties with a mean M
and a range R, random values of the properties are uniformly
distributed in the interval [M - R/2, M + R/2]. */
//随机数
@property unsigned int seed;
2.CAEmitterCell (粒子样式)
CAEmitterCell
继承于 : NSObject
属性介绍:
/* The name of the cell. Used to construct key paths. Defaults to nil. */
// 这个和uitableviewcell的Identifier的意义差不多
@property(nullable, copy) NSString *name;
/* Controls whether or not cells from this emitter are rendered. */
// 控制一个CAEmittercell 能不能够被使用
@property(getter=isEnabled) BOOL enabled;
/* The number of emitted objects created every second. Default value is
* zero. Animatable. */
//产生率
@property float birthRate;
/* The lifetime of each emitted object in seconds, specified as a mean
* value and a range about the mean. Both values default to zero.
* Animatable. */
//粒子的寿命
@property float lifetime;
@property float lifetimeRange; //寿命范围
/* The orientation of the emission angle in radians, relative to the
* natural orientation angle of the emission shape. Note that latitude
* here is what is typically called colatitude, the zenith or phi, the
* angle from the z-axis. Similarly longitude is the angle in the
* xy-plane from the x-axis, often referred to as the azimuth or
* theta. Both values default to zero, which translates to no change
* relative to the emission shape's direction. Both values are
* animatable. */
//粒子的纬度 emissionLatitude : x 与 z轴的夹角:
@property CGFloat emissionLatitude;
// 经度: x 与 y轴的夹角:
@property CGFloat emissionLongitude;
/* An angle (in radians) defining a cone around the emission angle.
* Emitted objects are uniformly distributed across this cone. Defaults
* to zero. Animatable. */
//发射范围
@property CGFloat emissionRange;
/* The initial mean velocity of each emitted object, and its range. Both
* values default to zero. Animatable. */
// 速度与速度范围
@property CGFloat velocity;
@property CGFloat velocityRange;
/* The acceleration vector applied to emitted objects. Defaults to
* (0, 0, 0). Animatable. */
// 加速度
@property CGFloat xAcceleration;
@property CGFloat yAcceleration;
@property CGFloat zAcceleration;
/* The scale factor applied to each emitted object, defined as mean and
* range about the mean. Scale defaults to one, range to zero.
* Animatable. */
//缩放 缩放范围 缩放速度
@property CGFloat scale;
@property CGFloat scaleRange;
@property CGFloat scaleSpeed;
/* The rotation speed applied to each emitted object, defined as mean
* and range about the mean. Defaults to zero. Animatable. */
// 旋转
@property CGFloat spin;
@property CGFloat spinRange;
/* The mean color of each emitted object, and the range from that mean
* color. `color' defaults to opaque white, `colorRange' to (0, 0, 0,
* 0). Animatable. */
//颜色
@property(nullable) CGColorRef color;
@property float redRange;
@property float greenRange;
@property float blueRange;
@property float alphaRange;
/* The speed at which color components of emitted objects change over
* their lifetime, defined as the rate of change per second. Defaults
* to (0, 0, 0, 0). Animatable. */
@property float redSpeed;
@property float greenSpeed;
@property float blueSpeed;
@property float alphaSpeed;
/* The cell contents, typically a CGImageRef. Defaults to nil.
* Animatable. */
// 需要用图片展示粒子的时候需要用到这个
@property(nullable, strong) id contents;
/* The sub-rectangle of the contents image that will be drawn. See
* CALayer.h for more details. Defaults to the unit rectangle [0 0 1 1].
* Animatable. */
@property CGRect contentsRect;
/* Defines the scale factor applied to the contents of the cell. See
* CALayer.h for more details. */
@property CGFloat contentsScale;
/* The filter parameters used when rendering the `contents' image. See
* CALayer.h for more details. */
@property(copy) NSString *minificationFilter;
@property(copy) NSString *magnificationFilter;
@property float minificationFilterBias;
/* An array containing the sub-cells of this cell, or nil (the default
* value). When non-nil each particle emitted by the cell will act as
* an emitter for each of the cell's sub-cells. The emission point is
* the current particle position and the emission angle is relative to
* the current direction of the particle. Animatable. */
//粒子里面嵌套粒子
@property(nullable, copy) NSArray<CAEmitterCell *> *emitterCells;
/* Inherited attributes similar to in layers. */
@property(nullable, copy) NSDictionary *style;
demo
- (void)setupEmitter{
// 1. 设置CAEmitterLayer
CAEmitterLayer * colorBallLayer = [CAEmitterLayer layer];
[self.view.layer addSublayer:colorBallLayer];
self.colorBallLayer = colorBallLayer;
//发射源的尺寸大小
CGSize size = CGSizeMake(100, 100);
// colorBallLayer.emitterSize = self.view.frame.size;
colorBallLayer.emitterSize = size;
//发射源的形状
colorBallLayer.emitterShape = kCAEmitterLayerPoint;
//发射模式
colorBallLayer.emitterMode = kCAEmitterLayerPoints;
//粒子发射形状的中心点
colorBallLayer.emitterPosition = CGPointMake(self.view.layer.bounds.size.width/2, 0.f);
// 2. 配置CAEmitterCell
CAEmitterCell * colorBallCell = [CAEmitterCell emitterCell];
//粒子名称
colorBallCell.name = @"colorBallCell";
//粒子产生率,默认为0
colorBallCell.birthRate = 3;
//粒子生命周期
colorBallCell.lifetime = 4;
//粒子速度,默认为0
colorBallCell.velocity = 10;
//粒子速度平均量
colorBallCell.velocityRange = 10;
//x,y,z方向上的加速度分量,三者默认都是0
colorBallCell.yAcceleration = 40;
colorBallCell.xAcceleration = -10;
colorBallCell.zAcceleration = 20;
//指定纬度,纬度角代表了在x-z轴平面坐标系中与x轴之间的夹角,默认0:
colorBallCell.emissionLongitude = M_PI; // 向左
//发射角度范围,默认0,以锥形分布开的发射角度。角度用弧度制。粒子均匀分布在这个锥形范围内;
colorBallCell.emissionRange = M_PI_4; // 围绕X轴向左90度
// 缩放比例, 默认是1
colorBallCell.scale = 1;
// 缩放比例范围,默认是0
colorBallCell.scaleRange = 0.1;
// 在生命周期内的缩放速度,默认是0
colorBallCell.scaleSpeed = 0.02;
// 粒子的内容,为CGImageRef的对象
colorBallCell.contents = (id)[[UIImage imageNamed:@"gxq"] CGImage];//gxq , circle_white
//颜色
colorBallCell.color = [[UIColor colorWithRed:0.5 green:0.f blue:0.5 alpha:1.f] CGColor];
//粒子颜色red,green,blue,alpha能改变的范围,默认0
colorBallCell.redRange = 1.f;
colorBallCell.greenRange = 1.f;
colorBallCell.alphaRange = 0.8;
// 粒子颜色red,green,blue,alpha在生命周期内的改变速度,默认都是0
colorBallCell.blueSpeed = 1.f;
colorBallCell.alphaSpeed = -0.1f;
// 添加
colorBallLayer.emitterCells = @[colorBallCell];
}
//以下代码实现移动发射源位置
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [self locationFromTouchEvent:event];
NSLog(@"point:%@",NSStringFromCGPoint(point));
[self setBallInPsition:point];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [self locationFromTouchEvent:event];
[self setBallInPsition:point];
}
/**
* 获取手指所在点
*/
- (CGPoint)locationFromTouchEvent:(UIEvent *)event{
UITouch * touch = [[event allTouches] anyObject];
return [touch locationInView:self.view];
}
/**
* 移动发射源到某个点上
*/
- (void)setBallInPsition:(CGPoint)position{
//创建基础动画
// CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath:@"emitterCells.colorBallCell.scale"];//emitterCells.colorBallCell.scale
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"emitterCells.colorBallCell.backgroundColor"];
anim.fromValue = CFBridgingRelease(([UIColor redColor].CGColor));
anim.toValue = CFBridgingRelease(([UIColor blueColor].CGColor));
//fromValue
// anim.fromValue = @0.2f;
//// //toValue
// anim.toValue = @0.5f;
self.colorBallLayer.emitterCells.firstObject.color = [[UIColor colorWithRed:0.5 green:0.f blue:0.5 alpha:1.f] CGColor];
// 粒子颜色red,green,blue,alpha能改变的范围,默认0
self.colorBallLayer.emitterCells.firstObject.redRange = 1.f;
self.colorBallLayer.emitterCells.firstObject.greenRange = 1.f;
self.colorBallLayer.emitterCells.firstObject.alphaRange = 0.8;
// 粒子颜色red,green,blue,alpha在生命周期内的改变速度,默认都是0
// colorBallCell.blueSpeed = 1.f;
self.colorBallLayer.emitterCells.firstObject.alphaSpeed = -0.1f;
//duration
anim.duration = 10.0f;
//线性起搏,使动画在其持续时间内均匀地发生
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
// 用事务包装隐式动画
[CATransaction begin];
//设置是否禁止由于该事务组内的属性更改而触发的操作。
[CATransaction setDisableActions:YES];
//为colorBallLayer 添加动画
[self.colorBallLayer addAnimation:anim forKey:nil];
//为colorBallLayer 指定位置添加动画效果
self.colorBallLayer.emitterPosition = position;
//提交动画
[CATransaction commit];
}
网友评论