当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS 粒子系统 (CAEmitterLayer)实例详解

IOS 粒子系统 (CAEmitterLayer)实例详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

一、系统剖析

在uikit中,粒子系统由两部分组成:

·      一个或多个caemittercells:发射器电池可以看作是单个粒子的原型(例如,一个单一的粉扑在一团烟雾)。当散发出一个粒子,uikit根据这个发射粒子和定义的基础上创建一个随机粒子。此原型包括一些属性来控制粒子的图片,颜色,方向,运动,缩放比例和生命周期。

·      一个或多个caemitterlayers,但通常只有一个:这个发射的层主要控制粒子的形状(例如,一个点,矩形或圆形)和发射的位置(例如,在矩形内,或边缘)。这个层具有全局的乘法器,可以施加到系统内的caemittercells。这些给你一个简单的方法覆盖的所有粒子的变化。比如一个人为的例子将改变x雨来模拟风的速度。

基础是简单的,但这些参数却是相当微妙的。caemitterlayer有超过30种不同的参数进行自定义粒子的行为。下面,我就拼出来的一些特殊问题

二、不可测性

1、是什么让粒子系统成为一个随机的系统?

caemittercell的属性一般有两个参数:一个均值和一个“cone”,比如velocity 和velocityrange。

默认情况下,这个“cone”是0,这就以为着所有粒子将具有相同的速度。通过改变这个“cone”,每个发射粒子会随机被扰动获得一个这个“cone”范围内的值。这点在apple官方文档caemitterlayer documentation:  有讲解:

each layer has its ownrandom number generator state. emitter cell properties that are defined as amean and a range, such as a cell's speed, the value of the properties areuniformly distributed in the interval [m - r/2, m + r/2].

2、发射的方向

caemittercells有一个velocity(速度)的属性,这意味着发送方向上的速度。实际上这个发射的方向是通过emissionlongitude属性定义的。apple这样阐述的:

the emission longitude is theorientation of the emission angle in the xy-plane. it is also often referred toas the azimuth.

三、代码

- (void)viewdidload 
{ 
 [super viewdidload]; 
 
 caemitterlayer *emitterlayer = [caemitterlayer layer]; 
 emitterlayer.emitterposition = self.view.center; 
 _emitterlayer = emitterlayer; 
 [self.view.layer addsublayer:emitterlayer]; 
  
 caemittercell *funnyemittercell = [caemittercell emittercell]; 
 funnyemittercell.contents = (id)[uiimage imagenamed:@"funny.jpg"].cgimage; 
 funnyemittercell.birthrate = 10.0; 
 funnyemittercell.velocity = 200.0; 
 funnyemittercell.lifetime = 5.0; 
 funnyemittercell.scale = 0.1; 
 funnyemittercell.name = @"funny"; 
 emitterlayer.emittercells = [nsarray arraywithobject:funnyemittercell]; 
 [self bumpangle]; 
  
 uilabel *anglelabel = [[uilabel alloc] initwithframe:cgrectmake(20, 20, 100, 30)]; 
 anglelabel.backgroundcolor = [uicolor clearcolor]; 
 [self.view addsubview:anglelabel]; 
 _anglelabel = anglelabel; 
} 
 
- (void) bumpangle { 
 nsnumber *emissionlongitude = [_emitterlayer valueforkeypath:@"emittercells.funny.emissionlongitude"]; 
 nsnumber *nextlongitude = [nsnumber numberwithfloat:[emissionlongitude floatvalue] + 0.02]; 
 [_emitterlayer setvalue:nextlongitude forkeypath:@"emittercells.funny.emissionlongitude"]; 
 _anglelabel.text = [nsstring stringwithformat:@"%.0f degrees", [nextlongitude floatvalue] * 180 / m_pi]; 
 [self performselector:@selector(bumpangle) withobject:nil afterdelay:0.1]; 
} 
  

代码设置/结构说明:

1、caemittercell

caemittercell *effectcell = [caemittercell emittercell];

effectcell 几个重要属性:

1).birthrate 顾名思义没有这个也就没有effectcell,这个必须要设置,具体含义是每秒某个点产生的effectcell数量

2).lifetime & lifetimerange 表示effectcell的生命周期,既在屏幕上的显示时间要多长。

3).contents 这个和calayer一样,只是用来设置图片

4).name 这个是当effectcell存在caeemitter 的emittercells中用来辨认的。用到setvalue forkeypath比较有用

5).velocity & velocityrange & emissionrange 表示cell向屏幕右边飞行的速度 & 在右边什么范围内飞行& +-角度扩散

6).把cell做成array放进caeemitter.emittercells里去。caeemitter.rendermode有个效果很不错,能变成火的就是kcaemitterlayeradditive

属性:

alpharange:  一个粒子的颜色alpha能改变的范围;

alphaspeed:粒子透明度在生命周期内的改变速度;

birthrate:粒子参数的速度乘数因子;

bluerange:一个粒子的颜色blue 能改变的范围;

bluespeed: 粒子blue在生命周期内的改变速度;

color:粒子的颜色

contents:是个cgimageref的对象,既粒子要展现的图片;

contentsrect:应该画在contents里的子rectangle:

emissionlatitude:发射的z轴方向的角度

emissionlongitude:x-y平面的发射方向

emissionrange;周围发射角度

emittercells:粒子发射的粒子

enabled:粒子是否被渲染

greenrange: 一个粒子的颜色green 能改变的范围;

greenspeed: 粒子green在生命周期内的改变速度;

lifetime:生命周期

lifetimerange:生命周期范围

magnificationfilter:不是很清楚好像增加自己的大小

minificatonfilter:减小自己的大小

minificationfilterbias:减小大小的因子

name:粒子的名字

redrange:一个粒子的颜色red 能改变的范围;

redspeed; 粒子red在生命周期内的改变速度;

scale:缩放比例:

scalerange:缩放比例范围;

scalespeed:缩放比例速度:

spin:子旋转角度

spinrange:子旋转角度范围

style:不是很清楚:

velocity:速度

velocityrange:速度范围

xacceleration:粒子x方向的加速度分量

yacceleration:粒子y方向的加速度分量

zacceleration:粒子z方向的加速度分量

2、caemitterlayer

caemitterlayer提供了一个基于core animation的粒子发射系统,粒子用caemittercell来初始化。粒子画在背景层盒边界上

属性:  

birthrate:粒子产生系数,默认1.0;

emittercells: 装着caemittercell对象的数组,被用于把粒子投放到layer上;

emitterdepth:决定粒子形状的深度联系:emittershape

emittermode:发射模式

nsstring * const kcaemitterlayerpoints;

nsstring * const kcaemitterlayeroutline;

nsstring * const kcaemitterlayersurface;

nsstring * const kcaemitterlayervolume;

emitterposition:发射位置

emittershape:发射源的形状:

nsstring * const kcaemitterlayerpoint;

nsstring * const kcaemitterlayerline;

nsstring * const kcaemitterlayerrectangle;

nsstring * const kcaemitterlayercuboid;

nsstring * const kcaemitterlayercircle;

nsstring * const kcaemitterlayersphere;

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:粒子速度

 以上就是对ios 粒子系统的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网