IOS粒子系统

 

CAEmitterLayer提供了一个基于Core Animation的粒子发射系统,粒子用CAEmitterCell来初始化。粒子画在背景层盒边界上

1、CAEmitterLayer:

emitterPosition//决定了粒子发射形状的中心点,
​
emitterSize    //则决定了粒子发射形状的大小,
​
emitterShape   //是粒子从什么形状发射出来,它并不是表示粒子自己的形状,
​
emitterMode    //决定了粒子的发射模式。
​
注:emitterPosition是所选emitterShape的中心点,例如对于矩形是对角线交点,对于圆形是圆心,对于直线是中点。而emitterSize则决定了矩形的大小,圆形的大小,直线的长度。
​
birthRate:      //粒子 产 生系数,默 认 1.0 ;
​
emitterCells:   //装着 CAEmitterCell 对 象的数 组 ,被用于把粒子投放到 layer 上;
​
emitterDepth:   //决定粒子形状的深度 联 系
​
emitterShape:   //发射源的形状:
​
       NSString *const kCAEmitterLayerPoint; // 点  
​
       NSString *const kCAEmitterLayerLine; // 直线 
​
       NSString *const kCAEmitterLayerRectangle; // 矩形 
​
       NSString *const kCAEmitterLayerCircle; // 圆形 
​
       NSString *const kCAEmitterLayerCuboid; // 3D rectangle 
​
       NSString *const kCAEmitterLayerSphere; // 3D circle
​
emitterMode: 发射模式 //进一步决定发射的区域是在发射形状的哪一部份
​
       NSString *const kCAEmitterLayerPoints; //顶点
​
       NSString *const kCAEmitterLayerOutline;  //轮廓 ,即边上
​
       NSString *const kCAEmitterLayerSurface; // 表面,即图形的面积内
​
       NSString *const kCAEmitterLayerVolume; // 容积,即3D图形的体积内                
​
emitterSize:      //发射源的尺寸大小
​
emitterZposition: //发射源的 z 坐 标 位置;
​
lifetime:         //粒子生命周期
​
preservesDepth:   
​
renderMode:       //渲染模式:
​
       NSString *const kCAEmitterLayerUnordered;
​
       NSString *const kCAEmitterLayerOldestFirst;
​
       NSString *const kCAEmitterLayerOldestLast;
​
       NSString *const kCAEmitterLayerBackToFront;
​
       NSString *const kCAEmitterLayerAdditive;
​
scale:           // 粒子的缩放比例:
​
seed:         //用于初始化随机数产生的种子
​
spin:           //自旋转速度
​
velocity:     //粒子速度

2、CAEmitterCell常用属性:

CAEmitterCell类代从CAEmitterLayer射出的粒子;
emittercell:        定义了粒子发射的方向。
name:               粒子的名字
birthrate:          粒子参数的速度乘数因子;
contents:           是个 CGImageRef的对象, 既粒子要展现的图片;
contentsRect:       应该画在 contents里的子 rectangle;
​
color:              粒子的颜色
redRange:          一个粒子的颜色red   能改变的范围;
redSpeed:           粒子 red在生命周期内的改变速度;
greenrange:         一个粒子的颜色green   能改变的范围;
greenSpeed:         粒子 green在生命周期内的改变速度;
blueRange:          一个粒子的颜色blue   能改变的范围;
blueSpeed:          粒子 blue在生命周期内的改变速度;
alphaRange:         一个粒子的颜色alpha能改变的范围;
alphaSpeed:         粒子透明度在生命周期内的改变速度;
​

emissionLatitude:   发射的 z轴方向的角度
emissionLongitude:  x-y平面的发射方向
emissionRange:      周围发射角度
emitterCells:       粒子发射的粒子
enabled:            粒子是否被渲染

lifetime:           生命周期
lifetimeRange:      生命周期范围
magnificationFilter:不是很清楚好像增加自己的大小
minificatonFilter:  减小自己的大小
minificationFilterBias:减小大小的因子
​
scale:              缩放比例:
scaleRange:         缩放比例范围;
scaleSpeed:         缩放比例速度:
spin:               子旋转角度
spinrange:          子旋转角度范围
style:              不是很清楚:
velocity:           速度
velocityRange:      速度范围
xAcceleration:      粒子 x方向的加速度分量
yAcceleration:      粒子 y方向的加速度分量
zAcceleration:      粒子 z方向的加速度分量
ClassMethods
defauleValueForKey: 更具健获得值;
emitterCell:       初始化方法
shouldArchiveValueForKey: 是否归档莫键值

3、雪花(撒花)粒子系统


- (void)addFragmentAnimation {
    _emitter = [CAEmitterLayer layer];
    _emitter.emitterPosition = CGPointMake(self.bounds.size.width / 2.0, -30);
    _emitter.emitterSize     = CGSizeMake(self.bounds.size.width, 0.0);;
    _emitter.emitterMode     = kCAEmitterLayerPoints;
    _emitter.emitterShape    = kCAEmitterLayerLine;
    
    CGFloat scaleRatio = [self cardScaleRatio];

    CAEmitterCell *(^generateEmitterCell)(NSString *) = ^(NSString *iconName) {
        CAEmitterCell *flowerFlake = [CAEmitterCell emitterCell];
        CGFloat random = (arc4random()%100) / 100.f;
        flowerFlake.birthRate = 3 * random;
        flowerFlake.lifetime = 120.0;
        flowerFlake.velocity = 100;
        flowerFlake.yAcceleration  = 75;
        flowerFlake.emissionRange  = -M_PI;
        flowerFlake.spinRange = 2 * M_PI;
        flowerFlake.scale          = 0.4 * scaleRatio;
        flowerFlake.contents = (id)[[UIImage imageForKey:iconName] CGImage];
        return flowerFlake;
    };
    
    NSMutableArray *emitterCells = [NSMutableArray array];
    for (int iconIndex = 1; iconIndex < 25; ++iconIndex) {
        NSString *iconName = [@"Fragment_" stringByAppendingString:[@(iconIndex) stringValue]];
        [emitterCells addObject:generateEmitterCell(iconName)];
    }
    _emitter.emitterCells = emitterCells;
    [self.layer addSublayer:_emitter];
}

 

猜你喜欢

转载自blog.csdn.net/weixin_39624536/article/details/83713751
今日推荐