iOS开发 下雪 下雨 落叶动画

直接上代码 ,原理就是发射粒子效果,控制粒子发射速度和方向等操作

下雨效果:

#import "RainViewController.h"

#import <MapKit/MapKit.h>

 

@interface RainViewController (){

    CAEmitterLayer * rainEmitterLayer;

    CAEmitterCell * rainCell;

    UISegmentedControl * rainSeg;

    MKMapView * mapview;

    UIView * bgView;

}

 

@end

 

@implementation RainViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    [self createUI];

    

    // Do any additional setup after loading the view.

}

 

- (void)createUI{

    

//    mapview = [[MKMapView alloc]initWithFrame:self.view.frame];

//    [self.view addSubview:mapview];

    

    bgView = [[UIView alloc]initWithFrame:self.view.frame];

    [self.view addSubview:bgView];

    

    // 返回按钮

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(15, 70, 60, 30);

    [btn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];

    [btn setTitle:@"返回" forState:UIControlStateNormal];

    btn.backgroundColor = [UIColor clearColor];

    btn.layer.cornerRadius = 5;

    btn.layer.borderColor = [[UIColor redColor]CGColor];

    btn.layer.borderWidth = 1;

    btn.tag = 1;

    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [self.view addSubview:btn];

    

    rainSeg = [[UISegmentedControl alloc]initWithItems:@[@"正常雨点",@"文艺雨点"]];

    rainSeg.frame = CGRectMake(self.view.frame.size.width / 2 - 60, 70, 120, 30);

    rainSeg.backgroundColor = [UIColor clearColor];

    rainSeg.tintColor = [UIColor redColor];

    [rainSeg addTarget:self action:@selector(changeRain:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:rainSeg];

}

 

- (void)backClick:(id)sender{

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

- (void)changeRain:(UISegmentedControl *)seg{

    switch (seg.selectedSegmentIndex) {

        case 0:

        {

            if (rainEmitterLayer) {

                [rainEmitterLayer removeFromSuperlayer];

            }

            [UIView animateWithDuration:0.8 animations:^{

                self->bgView.backgroundColor = [UIColor whiteColor];

                [self creatRainEmitterLayer];

            }];

        }

            break;

        case 1:

        {

            if (rainEmitterLayer) {

                [rainEmitterLayer removeFromSuperlayer];

            }

            [UIView animateWithDuration:0.8 animations:^{

                self->bgView.backgroundColor = [UIColor blackColor];

                [self createRainTwo];

            }];

           

        }

            break;

            

        default:

            break;

    }

}

    

 

- (void)creatRainEmitterLayer{

    // 使用背板视图来作为载体

    bgView.frame = self.view.frame;

    

    // 创建粒子发射器图层

    rainEmitterLayer = [CAEmitterLayer layer];

    // 设置属性

    // 发射源的形状 是枚举类型 ,因为是下雨 所以要作为 直线发射

    rainEmitterLayer.emitterShape = kCAEmitterLayerLine;

    // 发射模式

    rainEmitterLayer.emitterMode = kCAEmitterLayerSurface;

    // 发射源的size 据定了发射源的大小

    rainEmitterLayer.emitterSize = bgView.frame.size;

    // 发射源的位置 从屏幕上方往下发射

    rainEmitterLayer.emitterPosition = CGPointMake(bgView.frame.size.width*0.5, -20);

    // 渲染模式,暂不使用

    

    // 添加到目标视图的layer上

    [bgView.layer addSublayer:rainEmitterLayer];

    

    // 配置粒子Cell

    rainCell = [CAEmitterCell emitterCell];

    // 设置粒子图片

    rainCell.contents = (id)[[UIImage imageNamed:@"rain"]CGImage];

    

    // 组1

    // 设置粒子产生率

    rainCell.birthRate = 60.f;

    // 设置粒子生命周期

    rainCell.lifetime = 40.f;

    // 设置粒子持续时间,持续时间制约粒子生命周期

    rainCell.speed = 1.f;

    

    // 组2

    // 设置粒子速度

    rainCell.velocity = 20.f;

    // 设置粒子速度范围

    rainCell.velocityRange = 100.f;

    // 设置粒子下落加速度 Y轴

    rainCell.yAcceleration = 2000.f;

    

    // 组3 雨图片需要缩放统一大小 不能使用范围

    // 设置缩放比例

    rainCell.scale = 0.05f;

//    // 设置缩放范围

//    rainCell.scaleRange = 0.8f;

//    // 设置粒子存在期间缩放变化的速度

//    rainCell.scaleSpeed = 0.5f;

    

//    // 组4 雨点不需要旋转

//    // 设置粒子旋转速度

//    rainCell.spin = 0.5f;

//    // 设置粒子旋转范围

//    rainCell.spinRange = M_PI_2;

    

    // 设置粒子颜色 会附和图片修改图片颜色

    rainCell.color = [[UIColor whiteColor]CGColor];

    

    // 添加到粒子发射器

    rainEmitterLayer.emitterCells = @[rainCell];

}

 

 

- (void)createRainTwo{

    // 使用背板视图来作为载体

    mapview.frame = CGRectMake(-200, 0, self.view.frame.size.width + 400, self.view.frame.size.height);

    

    // 创建粒子发射器图层

    rainEmitterLayer = [CAEmitterLayer layer];

    // 设置属性

    // 发射源的形状 是枚举类型 ,因为是下雨 所以要作为 直线发射

    rainEmitterLayer.emitterShape = kCAEmitterLayerLine;

    // 发射模式

    rainEmitterLayer.emitterMode = kCAEmitterLayerSurface;

    // 发射源的size 据定了发射源的大小 (有倾斜度,我们需要加宽发射源)

    rainEmitterLayer.emitterSize = bgView.frame.size;

    // 发射源的位置 从屏幕上方往下发射

    rainEmitterLayer.emitterPosition = CGPointMake(bgView.frame.size.width*0.5, -20);

    // 渲染模式,暂不使用

    

    // 添加到目标视图的layer上

    [bgView.layer addSublayer:rainEmitterLayer];

    

    // 配置粒子Cell

    rainCell = [CAEmitterCell emitterCell];

    // 设置粒子图片

    rainCell.contents = (id)[[UIImage imageNamed:@"rain2"]CGImage];

    

    // 组1

    // 设置粒子产生率

    rainCell.birthRate = 60.f;

    // 设置粒子生命周期

    rainCell.lifetime = 40.f;

    // 设置粒子持续时间,持续时间制约粒子生命周期

    rainCell.speed = 5.f;

    

    // 组2

    // 设置粒子速度

    rainCell.velocity = 20.f;

    // 设置粒子速度范围

    rainCell.velocityRange = 100.f;

    // 设置粒子下落加速度 Y轴

    rainCell.yAcceleration = 1000.f;

    

    /**

     ⭐️ 因为所谓“文艺下雨”的图片雨点是向右下方倾斜的,所以我们在调整Y轴加速度的同时也要修改 X轴 的加速度,让图片看着是往右下方前进的

     ⭐️ 修改了 X轴 的数据之后发现动画会缺失,嗯,因为发射器的平面从屏幕的长方体变成了平行四边形,所以,出现了缺失这时候我们需要把 emitterlayer的frame调整的更大,具体来说应该是更宽

     */

    rainCell.xAcceleration = 300.f;

    

    // 组3 雨图片需要缩放统一大小 不能使用范围

    // 设置缩放比例

    rainCell.scale = 0.5f;

    

    // 设置粒子颜色 会附和图片修改图片颜色

    rainCell.color = [[UIColor whiteColor]CGColor];

    

    // 添加到粒子发射器

    rainEmitterLayer.emitterCells = @[rainCell];

}

 

下雪效果:

 

@interface QQShowViewController (){

    CAEmitterCell * explosionCell;

    CAEmitterLayer * explosionLayer;

    UIButton * btnaa;

}

 

@end

 

@implementation QQShowViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    /**

     ⭐️本页主要讲解的是 发射模式 和 发射位置

     */

    self.view.backgroundColor = [UIColor whiteColor];

    

    [self createUI];

    

    // Do any additional setup after loading the view.

}

 

- (void)createUI{

    // 返回按钮

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(15, 70, 60, 35);

    [btn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];

    [btn setTitle:@"返回" forState:UIControlStateNormal];

    btn.backgroundColor = [UIColor whiteColor];

    btn.layer.cornerRadius = 5;

    btn.layer.borderColor = [[UIColor redColor]CGColor];

    btn.layer.borderWidth = 1;

    btn.tag = 1;

    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [self.view addSubview:btn];

    

    btnaa = [UIButton buttonWithType:UIButtonTypeCustom];

    btnaa.frame = CGRectMake(self.view.frame.size.width / 2 - 30, 180, 60, 60);

    [btnaa addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];

    [btnaa setTitle:@"点击" forState:UIControlStateNormal];

    btnaa.backgroundColor = [UIColor whiteColor];

    btnaa.layer.cornerRadius = 30;

    btnaa.layer.borderColor = [[UIColor redColor]CGColor];

    btnaa.layer.borderWidth = 1;

    [btnaa setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [self.view addSubview:btnaa];

    

    UISegmentedControl * mitterShapeSeg = [[UISegmentedControl alloc]initWithItems:@[@"圆形",@"3D矩形",@"直线",@"点",@"矩形",@"球体"]];

    mitterShapeSeg.frame = CGRectMake(50, 280, self.view.frame.size.width - 100, 30);

    mitterShapeSeg.backgroundColor = [UIColor clearColor];

    mitterShapeSeg.tintColor = [UIColor redColor];

    [mitterShapeSeg addTarget:self action:@selector(changeShape:) forControlEvents:UIControlEventValueChanged];

    

    UISegmentedControl * mitterModeSege = [[UISegmentedControl alloc]initWithItems:@[@"轮廓发射",@"点发射",@"表面发射",@"指定位置发射"]];

    mitterModeSege.frame = CGRectMake(50, 330, self.view.frame.size.width - 100, 30);

    mitterModeSege.backgroundColor = [UIColor clearColor];

    mitterModeSege.tintColor = [UIColor redColor];

    [mitterModeSege addTarget:self action:@selector(changeModel:) forControlEvents:UIControlEventValueChanged];

    

    [self.view addSubview:mitterShapeSeg];

    [self.view addSubview:mitterModeSege];

    

    [self setupExplosion];

}

- (void)changeShape:(UISegmentedControl *)seg{

    switch (seg.selectedSegmentIndex) {

        case 0:

            //    粒子从一个以(发射位置)为中心的圆中发射出来。

            explosionLayer.emitterShape = kCAEmitterLayerCircle;

            break;

        case 1:

            //    粒子从一个相反角的长方体(3D矩形)发射。

            explosionLayer.emitterShape = kCAEmitterLayerCuboid;

            break;

        case 2:

            //    粒子从(发射位置)沿直线发射。

            explosionLayer.emitterShape = kCAEmitterLayerLine;

            break;

        case 3:

            //    粒子从一个点(发射位置)发射出来。

            explosionLayer.emitterShape = kCAEmitterLayerPoint;

            break;

        case 4:

            //    粒子从一个有相对角的矩形发射。

            explosionLayer.emitterShape = kCAEmitterLayerRectangle;

            break;

        case 5:

            //    粒子从一个以(发射位置)为中心的球体中发射出来。

            explosionLayer.emitterShape = kCAEmitterLayerSphere;

            break;

        default:

            break;

    }

    

}

 

- (void)changeModel:(UISegmentedControl *)seg{

    switch (seg.selectedSegmentIndex) {

        case 0:

            //    粒子从粒子发射器的轮廓发射出来

            explosionLayer.emitterMode = kCAEmitterLayerOutline;

            break;

        case 1:

            //    粒子从粒子发射器上的点发射出来。

            explosionLayer.emitterMode = kCAEmitterLayerPoints;

            break;

        case 2:

            //    粒子从粒子发射器的表面发射出来。

            explosionLayer.emitterMode = kCAEmitterLayerSurface;

            break;

        case 3:

            //    粒子从粒子发射器中的a位置发射。

            explosionLayer.emitterMode = kCAEmitterLayerVolume;

            break;

        

        default:

            break;

    }

}

 

- (void)setupExplosion{

    

    // 1. 粒子

    explosionCell = [CAEmitterCell emitterCell];

    explosionCell.name = @"explosionCell";

    explosionCell.alphaSpeed = -1.f;

    explosionCell.alphaRange = 0.10;

    explosionCell.lifetime = 2;

    explosionCell.lifetimeRange = 0.1;

    explosionCell.velocity = 40.f;

    explosionCell.velocityRange = 10.f;

    explosionCell.scale = 0.08;

    explosionCell.scaleRange = 0.02;

    explosionCell.contents = (id)[[UIImage imageNamed:@"spark_red"] CGImage];

    

    // 2.发射源

    explosionLayer = [CAEmitterLayer layer];

    [btnaa.layer addSublayer:explosionLayer];

    // 设定发射中心点 在按钮中心

    explosionLayer.position = CGPointMake(btnaa.bounds.size.width * 0.5, btnaa.bounds.size.height * 0.5);

    // 设定发射范围

    explosionLayer.emitterSize = CGSizeMake(btnaa.bounds.size.width + 25, btnaa.bounds.size.height + 25);

    // 设定发射方式 圆形

    explosionLayer.emitterShape = kCAEmitterLayerCircle;

    // 设定发射位置 在载体的边缘发射

    explosionLayer.emitterMode = kCAEmitterLayerOutline;

    explosionLayer.emitterCells = @[explosionCell];

}

 

- (void)action{

    btnaa.backgroundColor = [UIColor lightGrayColor];

    btnaa.userInteractionEnabled = NO;

    [explosionLayer setValue:@1000 forKeyPath:@"emitterCells.explosionCell.birthRate"];

    [self performSelector:@selector(stopAnimation) withObject:nil afterDelay:1.5];

}

 

- (void)stopAnimation{

    [explosionLayer setValue:@0 forKeyPath:@"emitterCells.explosionCell.birthRate"];

    [explosionLayer removeAllAnimations];

    btnaa.backgroundColor = [UIColor clearColor];

    btnaa.userInteractionEnabled = YES;

}

 

落叶效果

@interface ViewController (){

    CAEmitterCell * showCell;

    CAEmitterLayer * emitterLayer;

    UIButton * btn;

    UISegmentedControl * emitterChoseSeg;

}

 

@end

 

@implementation ViewController

 

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    // segment不选中的状态

    emitterChoseSeg.selectedSegmentIndex= -1;// 小于0即可

}

 

- (void)viewDidLoad {

 

    [self createUI];

    

    [super viewDidLoad];

    

    

    

    NSMutableArray * array = [[NSMutableArray alloc]init];

    [array addObject:@"aaa"];

    [array addObject:@"dsfdsfggd"];

    [array addObject:@"aafgewg"];

    

    for (int i = 0 ; i < array.count; i++) {

        NSLog(@"%@--%ld",[array objectAtIndex:i],[NSString stringWithFormat:@"%@",[array objectAtIndex:i]].length);

    }

    

    // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)createUI{

    

    // 讲解效果 心雨 按钮

    btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(100, 180, self.view.frame.size.width - 200, 45);

    [btn addTarget:self action:@selector(heartClick:) forControlEvents:UIControlEventTouchUpInside];

    [btn setTitle:@"心雨" forState:UIControlStateNormal];

    btn.backgroundColor = [UIColor redColor];

    btn.layer.cornerRadius = 5;

    btn.tag = 1;

    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [self.view addSubview:btn];

    

    // segment 选择器

    NSArray * array = [NSArray arrayWithObjects:@"下雨",@"下雪",@"发射效果",@"烟花",@"红包",@"放射小球", nil];

    emitterChoseSeg = [[UISegmentedControl alloc]initWithItems:array];

    emitterChoseSeg.frame = CGRectMake(15, 90, self.view.frame.size.width - 30, 30);

    emitterChoseSeg.tintColor = [UIColor redColor];

    [emitterChoseSeg addTarget:self action:@selector(changeEffectVC:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:emitterChoseSeg];

    

    [self createEmitterLayer];

}

 

- (void)changeEffectVC:(UISegmentedControl *)seg{

    switch (seg.selectedSegmentIndex) {

        case 0:{

            RainViewController * rvc = [[RainViewController alloc]init];

            [rvc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

            [self presentViewController:rvc animated:YES completion:nil];

        }

            break;

        case 1:{

            SnowViewController * svc = [[SnowViewController alloc]init];

            [svc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

            [self presentViewController:svc animated:YES completion:nil];

        }

            break;

        case 2:{

            QQShowViewController * qvc = [[QQShowViewController alloc]init];

            [qvc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

            [self presentViewController:qvc animated:YES completion:nil];

        }

            break;

        case 3:{

            FireWorksViewController * fvc = [[FireWorksViewController alloc]init];

            [fvc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

            [self presentViewController:fvc animated:YES completion:nil];

        }

            break;

        case 4:{

            RedPocketViewController * rvc = [[RedPocketViewController alloc]init];

            [rvc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

            [self presentViewController:rvc animated:YES completion:nil];

        }

            break;

        case 5:{

            ColorBallViewController * cvc = [[ColorBallViewController alloc]init];

            [cvc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

            [self presentViewController:cvc animated:YES completion:nil];

        }

            break;

            

        default:

            break;

    }

}

 

 

- (CABasicAnimation *)showEmitterLayerWithBegain:(BOOL)isbegain{

    CABasicAnimation * animate = [CABasicAnimation animationWithKeyPath:@"opacity"];

    animate.duration = 0.8f;

    

    if (isbegain) {

        [animate setFromValue:@0.f];

        [animate setToValue:@1.f];

    }else{

        [animate setFromValue:@1.0f];

        [animate setToValue:@0.f];

    }

    //   ②.4设置填充模式

    animate.fillMode = kCAFillModeForwards;

    animate.removedOnCompletion = NO;

    return  animate;

}

 

- (void)heartClick:(UIButton *)sender {

    if (sender.tag %2 == 0) {

        [emitterLayer addAnimation:[self showEmitterLayerWithBegain:NO] forKey:nil];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            self->emitterLayer.emitterCells = nil;

        });

    }else{

        [emitterLayer addAnimation:[self showEmitterLayerWithBegain:YES] forKey:nil];

        emitterLayer.emitterCells = @[showCell];

    }

    btn.tag = sender.tag + 1;

}

 

- (void)createEmitterLayer{

    /**

     CAEmitterLayer 发射、动画和呈现粒子系统的层

     粒子,由CAEmitterCell实例定义,绘制在图层背景颜色和边框上方。

     

     具体属性清单:

     NSArray<CAEmitterCell *> *emitterCells; // 用来装粒子的数组

     float birthRate; // 粒子产生系数,默认1.0

     float lifetime; // 粒子的生命周期系数, 默认1.0

     CGPoint emitterPosition; // 决定了粒子发射形状的中心点

     CGFloat emitterZPosition;

     CGSize emitterSize; // 发射源的尺寸大小

     CGFloat emitterDepth;

     NSString *emitterShape; // 发射源的形状

     NSString *emitterMode; // 发射模式

     NSString *renderMode; // 渲染模式

     BOOL preservesDepth;

     float velocity; // 粒子速度系数, 默认1.0

     float scale; // 粒子的缩放比例系数, 默认1.0

     float spin; // 粒子的自旋转速度系数, 默认1.0

     unsigned int seed; // 随机数发生器

     

     */

    

    // 使用方式:

    // 1.创建CAEmitterLayer

    emitterLayer = [CAEmitterLayer layer];

    

    // 2.设置CAEmitterLayer的属性(最主要的是前四个)

    // 发射源的形状 是枚举类型

    emitterLayer.emitterShape = kCAEmitterLayerLine;

    //    kCAEmitterLayerCircle

    //    粒子从一个以(发射位置)为中心的圆中发射出来。

    //    kCAEmitterLayerCuboid

    //    粒子从一个相反角的长方体(3D矩形)发射。

    //    kCAEmitterLayerLine

    //    粒子从(发射位置)沿直线发射。

    //    kCAEmitterLayerPoint

    //    粒子从一个点(发射位置)发射出来。

    //    kCAEmitterLayerRectangle

    //    粒子从一个有相对角的矩形发射。

    //    kCAEmitterLayerSphere

    //    粒子从一个以(发射位置)为中心的球体中发射出来。

    

    

    // 发射模式 枚举类型

    emitterLayer.emitterMode = kCAEmitterLayerSurface;

    //    kCAEmitterLayerOutline

    //    粒子从粒子发射器的轮廓发射出来。

    //    kCAEmitterLayerPoints

    //    粒子从粒子发射器上的点发射出来。

    //    kCAEmitterLayerSurface

    //    粒子从粒子发射器的表面发射出来。

    //    kCAEmitterLayerVolume

    //    粒子从粒子发射器中的a位置发射。

    

    // 发射源的size 决定了发射源的大小,如果做了倾斜或者便宜屏幕宽度是不够的,那时候就需要自定义

    emitterLayer.emitterSize = self.view.frame.size;

    // 发射源的位置

    emitterLayer.emitterPosition = CGPointMake(self.view.bounds.size.width * 0.5, -10);

    // 渲染模式 枚举类型 (当前选择最好,可以让重叠的部分高亮)

    emitterLayer.renderMode = kCAEmitterLayerAdditive;

    //  kCAEmitterLayerOldestFirst;

    //  kCAEmitterLayerOldestLast;

    //  kCAEmitterLayerBackToFront;

    //  kCAEmitterLayerAdditive;

    

    // 3.添加到目标视图的layer上

    [self.view.layer addSublayer:emitterLayer];

    

    /**

     CAEmitterCell 发射的粒子的定义

     CAEmitterCell类表示CAEmitterLayer对象发出的一个粒子源。发射单元定义发射粒子的方向和性质。发射器细胞可以有一系列子细胞,这些子细胞可以让粒子自己发射粒子。

     

     */

    

    // 4. 配置cell

    showCell = [CAEmitterCell emitterCell];

    showCell.contents = (id)[[UIImage imageNamed:@"love_red@3x"] CGImage];  // 粒子的内容 是CGImageRef类型的

    

    /**

     具体属性清单:

     NSString *name; // 粒子名字, 默认为nil

     BOOL enabled;

     float birthRate; // 粒子的产生率,默认0

     float lifetime; // 粒子的生命周期,以秒为单位。默认0

     float lifetimeRange; // 粒子的生命周期的范围,以秒为单位。默认0

     CGFloat emissionLatitude;// 指定纬度,纬度角代表了在x-z轴平面座标系中与x轴之间的夹角,默认0:

     CGFloat emissionLongitude; // 指定经度,经度角代表了在x-y轴平面座标系中与x轴之间的夹角,默认0:

     CGFloat emissionRange; //发射角度范围,默认0,以锥形分布开的发射角度。角度用弧度制。粒子均匀分布在这个锥形范围内;

     CGFloat velocity; // 速度和速度范围,两者默认0

     CGFloat velocityRange;

     CGFloat xAcceleration; // x,y,z方向上的加速度分量,三者默认都是0

     CGFloat yAcceleration;

     CGFloat zAcceleration;

     CGFloat scale; // 缩放比例, 默认是1

     CGFloat scaleRange; // 缩放比例范围,默认是0

     CGFloat scaleSpeed; // 在生命周期内的缩放速度,默认是0

     CGFloat spin; // 粒子的平均旋转速度,默认是0

     CGFloat spinRange; // 自旋转角度范围,弧度制,默认是0

     CGColorRef color; // 粒子的颜色,默认白色

     float redRange; // 粒子颜色red,green,blue,alpha能改变的范围,默认0

     float greenRange;

     float blueRange;

     float alphaRange;

     float redSpeed; // 粒子颜色red,green,blue,alpha在生命周期内的改变速度,默认都是0

     float greenSpeed;

     float blueSpeed;

     float alphaSpeed;

     id contents; // 粒子的内容,为CGImageRef的对象

     CGRect contentsRect;

     CGFloat contentsScale;

     NSString *minificationFilter;

     NSString *magnificationFilter;

     float minificationFilterBias;

     NSArray<CAEmitterCell *> *emitterCells; // 粒子里面的粒子

     NSDictionary *style;

     

     */

    

    // 粒子的产生率

    showCell.birthRate = 25.f;

    // 粒子的生命周期,以秒为单位。

    showCell.lifetime = 100.f;

    // 粒子的生命周期的范围,以秒为单位。默认0

    showCell.lifetimeRange = 25.f;

    // 速度,制约粒子生命周期

    // 官方解释为:指定时间如何从父时间空间映射到接收者的时间空间。「必需的」

    // 值越大粒子消失的越快

    showCell.speed = 2.f;

    

    // 速度

    showCell.velocity = 20.f;

    // 速度范围

    showCell.velocityRange = 80.f;

    // 三轴加速度(一般使用Y值)

    showCell.yAcceleration = 90.f;

    //    showCell.xAcceleration = 20.f;

    //    showCell.zAcceleration = 10.f;

    

    // 缩放比列

    showCell.scale = 0.3;

    // 缩放范围

    showCell.scaleRange = 0.5;

    // 在粒子的生命周期内尺度变化的速度。可以做成动画。

    showCell.scaleSpeed = 0.02;

    

    // 设置经纬度(方向)(⭐️⭐️需要在 kCAEmitterLayerPoint 的情况下搭配使用)

    //    showCell.emissionLongitude = M_PI; // 上(-M_PI_2) 下(M_PI_2) 左(M_PI) 右(M_PI_4) 通常使用这个

    //    // showCell.emissionLatitude = -M_PI_2; // 前(-M_PI_2) 后(M_PI_2) 左(M_PI) 右(M_PI_4)

    //    // 设置角度,控制发射范围(受到经纬度的影响)

    //    showCell.emissionRange = M_PI / 6; // 围绕X轴向左90度

    

    // 设置自转速度

    showCell.spin = 0.f;

    // 设置自转方向

    showCell.spinRange = M_PI;

    

    // 设置颜色(⭐️⭐️直接影响到图片的原有颜色)

    //    snowCell.color = [[UIColor colorWithRed:0.5 green:0.f blue:0.5 alpha:1.f] CGColor];

    // 粒子在三个色相中以及可见度的变化程度

    showCell.redRange = 10.f;

    showCell.greenRange = 20.f;

    showCell.blueRange = 30.f;

    

    showCell.redSpeed = 1.f;

    showCell.greenSpeed = 1.f;

    showCell.blueSpeed = 1.f;

    

    showCell.alphaRange = 0.8;

    showCell.alphaSpeed = -0.1f;

    

    // 5.粒子添加到CAEmitterLayer上

//    emitterLayer.emitterCells = @[showCell];

    

    /**

     ⭐️⭐️

     总结:

     

     想要做好各种粒子发射动画,最关键的几个点如下:

     

     layer需要注意的:

     // 发射源的形状 是枚举类型

     emitterLayer.emitterShape = kCAEmitterLayerLine;

     // 发射模式 枚举类型

     emitterLayer.emitterMode = kCAEmitterLayerSurface;

     

     cell需要注意的:

     // 设置经纬度(方向)(⭐️⭐️需要在 kCAEmitterLayerPoint 的情况下搭配使用)

     showCell.emissionLongitude = M_PI; // 上(-M_PI_2) 下(M_PI_2) 左(M_PI) 右(M_PI_4) 通常使用这个

     showCell.emissionLatitude = -M_PI_2; // 前(-M_PI_2) 后(M_PI_2) 左(M_PI) 右(M_PI_4)

     // 设置角度,控制发射范围(受到经纬度的影响)

     showCell.emissionRange = M_PI / 6; // 围绕X轴向左90度

     

     */

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章