能产生粒子效果的CAEmitterLayer

2023-12-22 00:58

本文主要是介绍能产生粒子效果的CAEmitterLayer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转自:http://www.cnblogs.com/YouXianMing/p/3785876.html  仅供学习参考使用

下雪效果:

//
//  RootViewController.m
//  Cell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad
{[super viewDidLoad];self.view.backgroundColor  = [UIColor blackColor];// 创建粒子LayerCAEmitterLayer *snowEmitter = [CAEmitterLayer layer];// 粒子发射位置snowEmitter.emitterPosition = CGPointMake(120,20);// 发射源的尺寸大小snowEmitter.emitterSize = self.view.bounds.size;// 发射模式snowEmitter.emitterMode = kCAEmitterLayerSurface;// 发射源的形状snowEmitter.emitterShape = kCAEmitterLayerLine;// 创建雪花类型的粒子CAEmitterCell *snowflake = [CAEmitterCell emitterCell];// 粒子的名字snowflake.name = @"snow";// 粒子参数的速度乘数因子snowflake.birthRate = 1.0;snowflake.lifetime = 120.0;// 粒子速度snowflake.velocity =10.0;// 粒子的速度范围snowflake.velocityRange = 10;// 粒子y方向的加速度分量snowflake.yAcceleration = 2;// 周围发射角度snowflake.emissionRange = 0.5 * M_PI;// 子旋转角度范围snowflake.spinRange = 0.25 * M_PI;snowflake.contents = (id)[[UIImage imageNamed:@"snow"] CGImage];// 设置雪花形状的粒子的颜色snowflake.color = [[UIColor whiteColor] CGColor];snowflake.scaleRange = 0.6f;snowflake.scale = 0.7f;snowEmitter.shadowOpacity = 1.0;snowEmitter.shadowRadius = 0.0;snowEmitter.shadowOffset = CGSizeMake(0.0, 0.0);// 粒子边缘的颜色snowEmitter.shadowColor = [[UIColor whiteColor] CGColor];// 添加粒子snowEmitter.emitterCells = @[snowflake];// 将粒子Layer添加进图层中
    [self.view.layer addSublayer:snowEmitter];
}@end

烟花效果:

//
//  RootViewController.m
//  Cell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad
{[super viewDidLoad];self.view.backgroundColor  = [UIColor blackColor];// Cells spawn in the bottom, moving upCAEmitterLayer *fireworksEmitter = [CAEmitterLayer layer];CGRect viewBounds = self.view.layer.bounds;fireworksEmitter.emitterPosition = \CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height);fireworksEmitter.emitterSize    = CGSizeMake(viewBounds.size.width/2.0, 0.0);fireworksEmitter.emitterMode    = kCAEmitterLayerOutline;fireworksEmitter.emitterShape    = kCAEmitterLayerLine;fireworksEmitter.renderMode        = kCAEmitterLayerAdditive;fireworksEmitter.seed = (arc4random()%100)+1;// Create the rocketCAEmitterCell* rocket = [CAEmitterCell emitterCell];rocket.birthRate        = 1.0;rocket.emissionRange    = 0.25 * M_PI;  // some variation in anglerocket.velocity            = 380;rocket.velocityRange    = 100;rocket.yAcceleration    = 75;rocket.lifetime            = 1.02;    // we cannot set the birthrate < 1.0 for the burst
    rocket.contents            = (id) [[UIImage imageNamed:@"DazRing"] CGImage];rocket.scale            = 0.2;rocket.color            = [[UIColor redColor] CGColor];rocket.greenRange        = 1.0;        // different colorsrocket.redRange            = 1.0;rocket.blueRange        = 1.0;rocket.spinRange        = M_PI;        // slow spin// the burst object cannot be seen, but will spawn the sparks// we change the color here, since the sparks inherit its valueCAEmitterCell* burst = [CAEmitterCell emitterCell];burst.birthRate            = 1.0;        // at the end of travelburst.velocity            = 0;burst.scale                = 2.5;burst.redSpeed            =-1.5;        // shiftingburst.blueSpeed            =+1.5;        // shiftingburst.greenSpeed        =+1.0;        // shiftingburst.lifetime            = 0.35;// and finally, the sparksCAEmitterCell* spark = [CAEmitterCell emitterCell];spark.birthRate            = 400;spark.velocity            = 125;spark.emissionRange        = 2* M_PI;    // 360 degspark.yAcceleration        = 75;        // gravityspark.lifetime            = 3;spark.contents            = (id) [[UIImage imageNamed:@"snow"] CGImage];spark.scaleSpeed        =-0.2;spark.greenSpeed        =-0.1;spark.redSpeed            = 0.4;spark.blueSpeed            =-0.1;spark.alphaSpeed        =-0.25;spark.spin                = 2* M_PI;spark.spinRange            = 2* M_PI;// putting it togetherfireworksEmitter.emitterCells    = [NSArray arrayWithObject:rocket];rocket.emitterCells                = [NSArray arrayWithObject:burst];burst.emitterCells                = [NSArray arrayWithObject:spark];[self.view.layer addSublayer:fireworksEmitter];
}@end

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

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

@property float lifetime;

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

@property CGPoint emitterPosition;
@property CGFloat emitterZPosition;

 

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

 

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

@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;

 

 

你需要知道的非常重要的细节:

CAEmitterCell的动画属性挺多的,但有点奇葩的是,必须是通过KVC设置值才行的通,而且,动画结束时的值也必须通过KVC设置才有效......

复制代码
//
//  RootViewController.m
//  Cell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXGCD.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad
{[super viewDidLoad];self.view.backgroundColor  = [UIColor blackColor];// 创建粒子LayerCAEmitterLayer *snowEmitter = [CAEmitterLayer layer];// 粒子发射位置snowEmitter.emitterPosition = CGPointMake(120,20);// 发射源的尺寸大小snowEmitter.emitterSize = self.view.bounds.size;// 发射模式snowEmitter.emitterMode = kCAEmitterLayerSurface;// 发射源的形状snowEmitter.emitterShape = kCAEmitterLayerLine;// 创建雪花类型的粒子CAEmitterCell *snowflake = [CAEmitterCell emitterCell];// 粒子参数的速度乘数因子snowflake.birthRate = 20.0;snowflake.lifetime = 120.0;// 粒子速度snowflake.velocity =10.0;// 粒子的速度范围snowflake.velocityRange = 10;// 粒子y方向的加速度分量snowflake.yAcceleration = 200;// 周围发射角度snowflake.emissionRange = 0.5 * M_PI;// 子旋转角度范围snowflake.spinRange = 0.25 * M_PI;snowflake.contents = (id)[[UIImage imageNamed:@"snow"] CGImage];// 设置雪花形状的粒子的颜色snowflake.color = [[UIColor whiteColor] CGColor];snowEmitter.shadowOpacity = 1.0;snowEmitter.shadowRadius = 0.0;snowEmitter.shadowOffset = CGSizeMake(0.0, 0.0);// 粒子边缘的颜色snowEmitter.shadowColor = [[UIColor whiteColor] CGColor];// 粒子的名字snowflake.name = @"snow";// 添加粒子snowEmitter.emitterCells = @[snowflake];// 将粒子Layer添加进图层中
    [self.view.layer addSublayer:snowEmitter];// 7秒后执行[[GCDQueue mainQueue] execute:^{// 设置基本动画CABasicAnimation *ani = \[CABasicAnimation animationWithKeyPath:@"emitterCells.snow.birthRate"];ani.fromValue = @(20.0);ani.toValue   = @(0.0);ani.duration  = 12.f;// 设置结束时的值[snowEmitter setValue:[NSNumber numberWithFloat:0]forKeyPath:@"emitterCells.snow.birthRate"];// 添加动画
        [snowEmitter addAnimation:ani forKey:nil];} afterDelay:NSEC_PER_SEC * 7];
}@end

下雪效果:

复制代码
//
//  RootViewController.m
//  Cell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad
{[super viewDidLoad];self.view.backgroundColor  = [UIColor blackColor];// 创建粒子LayerCAEmitterLayer *snowEmitter = [CAEmitterLayer layer];// 粒子发射位置snowEmitter.emitterPosition = CGPointMake(120,20);// 发射源的尺寸大小snowEmitter.emitterSize = self.view.bounds.size;// 发射模式snowEmitter.emitterMode = kCAEmitterLayerSurface;// 发射源的形状snowEmitter.emitterShape = kCAEmitterLayerLine;// 创建雪花类型的粒子CAEmitterCell *snowflake = [CAEmitterCell emitterCell];// 粒子的名字snowflake.name = @"snow";// 粒子参数的速度乘数因子snowflake.birthRate = 1.0;snowflake.lifetime = 120.0;// 粒子速度snowflake.velocity =10.0;// 粒子的速度范围snowflake.velocityRange = 10;// 粒子y方向的加速度分量snowflake.yAcceleration = 2;// 周围发射角度snowflake.emissionRange = 0.5 * M_PI;// 子旋转角度范围snowflake.spinRange = 0.25 * M_PI;snowflake.contents = (id)[[UIImage imageNamed:@"snow"] CGImage];// 设置雪花形状的粒子的颜色snowflake.color = [[UIColor whiteColor] CGColor];snowflake.scaleRange = 0.6f;snowflake.scale = 0.7f;snowEmitter.shadowOpacity = 1.0;snowEmitter.shadowRadius = 0.0;snowEmitter.shadowOffset = CGSizeMake(0.0, 0.0);// 粒子边缘的颜色snowEmitter.shadowColor = [[UIColor whiteColor] CGColor];// 添加粒子snowEmitter.emitterCells = @[snowflake];// 将粒子Layer添加进图层中
    [self.view.layer addSublayer:snowEmitter];
}@end
复制代码

烟花效果:

复制代码
//
//  RootViewController.m
//  Cell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad
{[super viewDidLoad];self.view.backgroundColor  = [UIColor blackColor];// Cells spawn in the bottom, moving upCAEmitterLayer *fireworksEmitter = [CAEmitterLayer layer];CGRect viewBounds = self.view.layer.bounds;fireworksEmitter.emitterPosition = \CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height);fireworksEmitter.emitterSize    = CGSizeMake(viewBounds.size.width/2.0, 0.0);fireworksEmitter.emitterMode    = kCAEmitterLayerOutline;fireworksEmitter.emitterShape    = kCAEmitterLayerLine;fireworksEmitter.renderMode        = kCAEmitterLayerAdditive;fireworksEmitter.seed = (arc4random()%100)+1;// Create the rocketCAEmitterCell* rocket = [CAEmitterCell emitterCell];rocket.birthRate        = 1.0;rocket.emissionRange    = 0.25 * M_PI;  // some variation in anglerocket.velocity            = 380;rocket.velocityRange    = 100;rocket.yAcceleration    = 75;rocket.lifetime            = 1.02;    // we cannot set the birthrate < 1.0 for the burst
    rocket.contents            = (id) [[UIImage imageNamed:@"DazRing"] CGImage];rocket.scale            = 0.2;rocket.color            = [[UIColor redColor] CGColor];rocket.greenRange        = 1.0;        // different colorsrocket.redRange            = 1.0;rocket.blueRange        = 1.0;rocket.spinRange        = M_PI;        // slow spin// the burst object cannot be seen, but will spawn the sparks// we change the color here, since the sparks inherit its valueCAEmitterCell* burst = [CAEmitterCell emitterCell];burst.birthRate            = 1.0;        // at the end of travelburst.velocity            = 0;burst.scale                = 2.5;burst.redSpeed            =-1.5;        // shiftingburst.blueSpeed            =+1.5;        // shiftingburst.greenSpeed        =+1.0;        // shiftingburst.lifetime            = 0.35;// and finally, the sparksCAEmitterCell* spark = [CAEmitterCell emitterCell];spark.birthRate            = 400;spark.velocity            = 125;spark.emissionRange        = 2* M_PI;    // 360 degspark.yAcceleration        = 75;        // gravityspark.lifetime            = 3;spark.contents            = (id) [[UIImage imageNamed:@"snow"] CGImage];spark.scaleSpeed        =-0.2;spark.greenSpeed        =-0.1;spark.redSpeed            = 0.4;spark.blueSpeed            =-0.1;spark.alphaSpeed        =-0.25;spark.spin                = 2* M_PI;spark.spinRange            = 2* M_PI;// putting it togetherfireworksEmitter.emitterCells    = [NSArray arrayWithObject:rocket];rocket.emitterCells                = [NSArray arrayWithObject:burst];burst.emitterCells                = [NSArray arrayWithObject:spark];[self.view.layer addSublayer:fireworksEmitter];
}@end
复制代码

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

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

@property float lifetime;

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

@property CGPoint emitterPosition;
@property CGFloat emitterZPosition;

 

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

 

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

@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;

 

 

你需要知道的非常重要的细节:

CAEmitterCell的动画属性挺多的,但有点奇葩的是,必须是通过KVC设置值才行的通,而且,动画结束时的值也必须通过KVC设置才有效......

复制代码
//
//  RootViewController.m
//  Cell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXGCD.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad
{[super viewDidLoad];self.view.backgroundColor  = [UIColor blackColor];// 创建粒子LayerCAEmitterLayer *snowEmitter = [CAEmitterLayer layer];// 粒子发射位置snowEmitter.emitterPosition = CGPointMake(120,20);// 发射源的尺寸大小snowEmitter.emitterSize = self.view.bounds.size;// 发射模式snowEmitter.emitterMode = kCAEmitterLayerSurface;// 发射源的形状snowEmitter.emitterShape = kCAEmitterLayerLine;// 创建雪花类型的粒子CAEmitterCell *snowflake = [CAEmitterCell emitterCell];// 粒子参数的速度乘数因子snowflake.birthRate = 20.0;snowflake.lifetime = 120.0;// 粒子速度snowflake.velocity =10.0;// 粒子的速度范围snowflake.velocityRange = 10;// 粒子y方向的加速度分量snowflake.yAcceleration = 200;// 周围发射角度snowflake.emissionRange = 0.5 * M_PI;// 子旋转角度范围snowflake.spinRange = 0.25 * M_PI;snowflake.contents = (id)[[UIImage imageNamed:@"snow"] CGImage];// 设置雪花形状的粒子的颜色snowflake.color = [[UIColor whiteColor] CGColor];snowEmitter.shadowOpacity = 1.0;snowEmitter.shadowRadius = 0.0;snowEmitter.shadowOffset = CGSizeMake(0.0, 0.0);// 粒子边缘的颜色snowEmitter.shadowColor = [[UIColor whiteColor] CGColor];// 粒子的名字snowflake.name = @"snow";// 添加粒子snowEmitter.emitterCells = @[snowflake];// 将粒子Layer添加进图层中
    [self.view.layer addSublayer:snowEmitter];// 7秒后执行[[GCDQueue mainQueue] execute:^{// 设置基本动画CABasicAnimation *ani = \[CABasicAnimation animationWithKeyPath:@"emitterCells.snow.birthRate"];ani.fromValue = @(20.0);ani.toValue   = @(0.0);ani.duration  = 12.f;// 设置结束时的值[snowEmitter setValue:[NSNumber numberWithFloat:0]forKeyPath:@"emitterCells.snow.birthRate"];// 添加动画
        [snowEmitter addAnimation:ani forKey:nil];} afterDelay:NSEC_PER_SEC * 7];
}@end
复制代码

这篇关于能产生粒子效果的CAEmitterLayer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/522101

相关文章

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

使用Python实现生命之轮Wheel of life效果

《使用Python实现生命之轮Wheeloflife效果》生命之轮Wheeloflife这一概念最初由SuccessMotivation®Institute,Inc.的创始人PaulJ.Meyer... 最近看一个生命之轮的视频,让我们珍惜时间,因为一生是有限的。使用python创建生命倒计时图表,珍惜时间

防近视护眼台灯什么牌子好?五款防近视效果好的护眼台灯推荐

在家里,灯具是属于离不开的家具,每个大大小小的地方都需要的照亮,所以一盏好灯是必不可少的,每个发挥着作用。而护眼台灯就起了一个保护眼睛,预防近视的作用。可以保护我们在学习,阅读的时候提供一个合适的光线环境,保护我们的眼睛。防近视护眼台灯什么牌子好?那我们怎么选择一个优秀的护眼台灯也是很重要,才能起到最大的护眼效果。下面五款防近视效果好的护眼台灯推荐: 一:六个推荐防近视效果好的护眼台灯的

MCU7.keil中build产生的hex文件解读

1.hex文件大致解读 闲来无事,查看了MCU6.用keil新建项目的hex文件 用FlexHex打开 给我的第一印象是:经过软件的解释之后,发现这些数据排列地十分整齐 :02000F0080FE71:03000000020003F8:0C000300787FE4F6D8FD75810702000F3D:00000001FF 把解释后的数据当作十六进制来观察 1.每一行数据

【Godot4.3】多边形的斜线填充效果基础实现

概述 图案(Pattern)填充是一个非常常见的效果。其中又以斜线填充最为简单。本篇就探讨在Godot4.3中如何使用Geometry2D和CanvasItem的绘图函数实现斜线填充效果。 基础思路 Geometry2D类提供了多边形和多边形以及多边形与折线的布尔运算。按照自然的思路,多边形的斜线填充应该属于“多边形与折线的布尔运算”范畴。 第一个问题是如何获得斜线,这条斜线应该满足什么样

Unity 资源 之 Super Confetti FX:点亮项目的璀璨粒子之光

Unity 资源 之 Super Confetti FX:点亮项目的璀璨粒子之光 一,前言二,资源包内容三,免费获取资源包 一,前言 在创意的世界里,每一个细节都能决定一个项目的独特魅力。今天,要向大家介绍一款令人惊艳的粒子效果包 ——Super Confetti FX。 二,资源包内容 💥充满活力与动态,是 Super Confetti FX 最显著的标签。它宛如一位