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度

     

     */

}

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