美文网首页
CoreAnimation 粒子效果CAEmitterLayer

CoreAnimation 粒子效果CAEmitterLayer

作者: MonKey_Money | 来源:发表于2020-08-04 11:34 被阅读0次

前沿

CAEmitterLayer是发射,设置动画和渲染粒子系统的层。在微信中给还有发送生日快乐,界面会出现下落的蛋糕,类似于这种的效果,可以用CAEmitterLayer实现。

属性

emitterCells

/* The array of emitter cells attached to the layer. Each object must
 * have the CAEmitterCell class. */
@property(nullable, copy) NSArray<CAEmitterCell *> *emitterCells;

emitterCells是可以为空,但是如果为空,我们只能看到普通的layer,不能看到粒子效果

birthRate :

 /* The birth rate of each cell is multiplied by this number to give the
 * actual number of particles created every second. Default value is one.
 * Animatable. */

@property float birthRate;

该属性和CAEmitterCell中的属性birthRate相乘,得到每秒产生多少粒子

lifetime

/* The cell lifetime range is multiplied by this value when particles are
 * created. Defaults to one. Animatable. */

@property float lifetime;

粒子的改属性乘以lifetime得出粒子的存活时间.

emitterPosition 和emitterZPosition

/* The center of the emission shape. Defaults to (0, 0, 0). Animatable. */

@property CGPoint emitterPosition;
@property CGFloat emitterZPosition;

发射形状的中心.

emitterSize 和emitterDepth

/* The size of the emission shape. Defaults to (0, 0, 0). Animatable.
 * Depending on the `emitterShape' property some of the values may be
 * ignored. */

@property CGSize emitterSize;
@property CGFloat emitterDepth;

发射形状的大小,默认为0,0,0,,会可能根据emitterShape忽略该值。

emitterShape

/* A string defining the type of emission shape used. Current options are:
 * `point' (the default), `line', `rectangle', `circle', `cuboid' and
 * `sphere'. */

@property(copy) CAEmitterLayerEmitterShape emitterShape;

字符串定义粒子发射的形状,当前选项是: 点(默认),线,矩形,圆,长方体和 球形

emitterMode

/* 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;

字符串定义如何相对于发射形状创建粒子。当前选项是“点”,“轮廓”,“表面”和“体积”(默认)

renderMode

/* 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;

字符串定义如何将粒子合成到图层图像中,如果两个cell位置重叠,怎么显示。

preservesDepth

/* 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;

velocity

/* Multiplies the cell-defined particle velocity. Defaults to one.
 * Animatable. */

@property float velocity;

乘以单元定义的粒子速度

scale

/* Multiplies the cell-defined particle scale. Defaults to one. Animatable. */
@property float scale;

乘以单元格定义的粒子比例

spin

/* Multiplies the cell-defined particle spin. Defaults to one. Animatable. */
@property float spin;

乘以单元格定义的粒子自旋

seed

/* 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;

下面一个demo

  CAEmitterLayer * colorBallLayer = [CAEmitterLayer layer];
    [self.view.layer addSublayer:colorBallLayer];
    self.colorBallLayer = colorBallLayer;
        //发射源的尺寸大小
    colorBallLayer.emitterSize = self.view.frame.size;
    //发射源的形状
    colorBallLayer.emitterShape = kCAEmitterLayerPoint;
    //发射模式
    colorBallLayer.emitterMode = kCAEmitterLayerPoints;
    //粒子发射形状的中心点
    colorBallLayer.emitterPosition = CGPointMake(self.view.layer.bounds.size.width, 0.f);
       // 2. 配置CAEmitterCell
    CAEmitterCell * colorBallCell = [CAEmitterCell emitterCell];
    //粒子名称
    colorBallCell.name = @"colorBallCell";
    //粒子产生率,默认为0
    colorBallCell.birthRate = 20.f;
    //粒子生命周期
    colorBallCell.lifetime = 10.f;
    //粒子速度,默认为0
    colorBallCell.velocity = 40.f;
    //粒子速度平均量
    colorBallCell.velocityRange = 100.f;
    //x,y,z方向上的加速度分量,三者默认都是0
    colorBallCell.yAcceleration = 15.f;
    //指定纬度,纬度角代表了在x-z轴平面坐标系中与x轴之间的夹角,默认0:
    colorBallCell.emissionLongitude = M_PI; // 向左
    //发射角度范围,默认0,以锥形分布开的发射角度。角度用弧度制。粒子均匀分布在这个锥形范围内;
    colorBallCell.emissionRange = M_PI_4; // 围绕X轴向左90度
    // 缩放比例, 默认是1
    colorBallCell.scale = 0.2;
    // 缩放比例范围,默认是0
    colorBallCell.scaleRange = 0.1;
    // 在生命周期内的缩放速度,默认是0
    colorBallCell.scaleSpeed = 0.02;
    // 粒子的内容,为CGImageRef的对象
    colorBallCell.contents = (id)[[UIImage imageNamed:@"circle_white"] CGImage];
    //颜色
    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];

相关文章

网友评论

      本文标题:CoreAnimation 粒子效果CAEmitterLayer

      本文链接:https://www.haomeiwen.com/subject/mwbirktx.html