IOS 粒子系統 (CAEmitterLayer)執行個體詳解_IOS

來源:互聯網
上載者:User

一、系統剖析

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

四、Demo下載

 以上就是對IOS 粒子系統的資料整理,後續繼續補充相關資料,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.