iOS實現「下雨下雪」動畫效果和「烟花」動畫效果

2021-10-08 23:03:55 字數 4832 閱讀 6348

一、效果展示

二、實現流程

func setupui()

self

.rainlayer = caemitterlayer.

init()

self

.imageview?

.layer.

addsublayer

(self

.rainlayer!

)// 發射形狀:線性

self

.rainlayer!

.emittershape = caemitterlayeremittershape.line

// 發射模式

self

.rainlayer!

.emittermode = caemitterlayeremittermode.su***ce

// 發射源大小

self

.rainlayer!

.emittersize =

self

.view.frame.size

// 發射源位置 y最好不要設定為0 最好<0

self

.rainlayer!

.emitterposition = cgpoint.

init

(x:self

.view.bounds.size.width *

0.5, y:-10

)

let snowcell = caemittercell.

init()

// 每秒產生的粒子數量的係數

snowcell.birthrate =

25.0

// 粒子內容

snowcell.contents = uiimage.

init

(named:

"rain_white")?

.cgimage

// 粒子的生命週期

snowcell.lifetime =

20.0

// speed粒子速度.圖層的速率。用於將父時間縮放為本地時間,例如,如果速率是2,則本地時間的進度是父時間的兩倍。預設值為1

snowcell.speed =

10.0

// 粒子速度係數, 預設1.0

snowcell.velocity =

10.0

// 每個發射物體的初始平均範圍,預設等於0

snowcell.velocityrange =

10.0

// 粒子在y方向的加速的

snowcell.yacceleration =

1000.0

// 粒子縮放比例: scale

snowcell.scale =

0.1// 粒子縮放比例範圍:scalerange

snowcell.scalerange =

0.0self

.rainlayer?

.emittercells =

[snowcell]

// mark: events responder

@ibaction func respondertorainbig

(_ sender: uibutton)

}@ibaction func responderttorainstop

(_ sender: uibutton)

else

}@ibaction func respondertorainsmall

(_ sender: uibutton)

}

一、效果展示

實現

self

.view.backgroundcolor = uicolor.black

// cell產生在底部,向上移動

let fireworkdemitter = caemitterlayer.

init()

fireworkdemitter.emitterposition = cgpoint.

init

(x:self

.view.frame.size.width/

2, y:

self

.view.frame.size.height)

fireworkdemitter.emittermode = caemitterlayeremittermode.outline

fireworkdemitter.emittershape = caemitterlayeremittershape.line

fireworkdemitter.rendermode = caemitterlayerrendermode.additive

fireworkdemitter.seed =

(arc4random()

%100)+

1// 建立火箭cell

let rocket = caemittercell.

init()

rocket.birthrate =

1 rocket.emissionrange =

cgfloat

(0.25

* double.pi)

; rocket.velocity =

300 rocket.velocityrange =

75 rocket.lifetime =

1.02

rocket.contents = uiimage.

init

(named:

"rain_white")?

.cgimage

rocket.scale =

0.5 rocket.scalerange =

0.5 rocket.color = uicolor.red.cgcolor

rocket.greenrange =

1.0 rocket.redrange =

1.0 rocket.bluerange =

1.0 rocket.spinrange =

cgfloat

(double.pi)

// 破裂物件不能被看到,但會產生火花

// 這裡改變顏色,因為火花繼承它的值

let firecell = caemittercell.

init()

firecell.birthrate =

1 firecell.velocity =

0 firecell.scale =

1 firecell.redspeed =

-1.5

firecell.bluespeed =

+1.5

firecell.greenspeed =

+1.5

firecell.lifetime =

0.34

// and finally, the sparks

let spark = caemittercell.

init()

// 炸開後產生400個小烟花

spark.birthrate =

400// 速度

spark.velocity =

125// 360度

spark.emissionrange =

cgfloat(2

* double.pi)

// 重力

spark.yacceleration =

40 spark.lifetime =

3

spark.contents = uiimage.

init

(named:

"rain_white")?

.cgimage

spark.scalespeed =

-0.2

spark.greenspeed =

-0.1

spark.redspeed =

+0.1

spark.bluespeed =

-0.1

spark.alphaspeed =

-0.25

spark.spin =

cgfloat(2

* double.pi)

spark.spinrange =

cgfloat(2

* double.pi)

fireworkdemitter.emittercells =

[rocket]

rocket.emittercells =

[firecell]

firecell.emittercells =

[spark]

;self

.view.layer.

addsublayer

(fireworkdemitter)

原生js canvas實現下雪效果

控制下雪 function snowfall snow this.maxflake snow.maxflake 200 最多片數 this.flakesize snow.flakesize 10 雪花形狀 this.fallspeed snow.fallspeed 1 墜落速度 相容寫法 reque...

iOS動畫效果和實現

動畫效果提供了狀態或頁面轉換時流暢的使用者體驗,在ios系統中,咱們不需要自己編寫繪製動畫的 core animation提供了豐富的api來實現你需要的動畫效果。uikit只用uiview來展示動畫,動畫支援uiview下面的這些屬性改變 1 commitanimations方式使用uiview動...

iOS幾種動畫效果的實現

做過乙個專案,裡面涉及到的動畫效果比較多,在這裡做個小小的總結。實現乙個物體在某段時間從乙個點移動到另乙個點。效果如下 動畫相關 如下 動畫模型 inte ce animationmodel nsobject property nonatomic,strong nsarray images prop...