亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

IOS動(dòng)畫效果源代碼整理(粒子、雪花、火焰、河流、蒸汽)

 更新時(shí)間:2018年01月26日 08:52:09   投稿:laozhang  
本篇文章給大家整理的IOS的關(guān)于動(dòng)畫的效果代碼整理,很多效果非常的好看,有興趣的學(xué)下。

學(xué)習(xí)神奇的粒子發(fā)射器,雪花紛紛落下的動(dòng)畫效果,就是通過CAEmitterLayer來實(shí)現(xiàn)的,這個(gè)layer還能創(chuàng)建火焰,河流,蒸汽的動(dòng)畫效果,常用于游戲開發(fā)。

Creating your emitter layer

let rect = CGRect(x: 0.0, y: -70.0, width: view.bounds.width, height: 50.0)
let emitter = CAEmitterLayer()
emitter.backgroundColor = UIColor.blueColor().CGColor
emitter.frame = rect
emitter.emitterShape = kCAEmitterLayerRectangle
view.layer.addSublayer(emitter)

代碼創(chuàng)建了CAEmitterLayer,并設(shè)置了發(fā)射源形狀emitterShape。

有幾個(gè)常用的emitterShape:

kCAEmitterLayerPoint:使所有粒子在同一點(diǎn)創(chuàng)建發(fā)射器的位置。這是一個(gè)很好的選擇用于火花或煙花,比如,你可以創(chuàng)建一個(gè)火花效應(yīng),通過創(chuàng)建所有的粒子在同一點(diǎn)上,使它們在不同的方向飛,然后消失。

kCAEmitterLayerLine:所有粒子沿發(fā)射架頂部的頂部。這是一個(gè)用于瀑布效應(yīng)的發(fā)射極的形狀;水粒子出現(xiàn)在瀑布的頂部邊緣。

kCAEmitterLayerRectangle:創(chuàng)建粒子隨機(jī)通過一個(gè)給定的矩形區(qū)域。

Adding an emitter frame

前面是設(shè)置了layer Frame,下面設(shè)置layer里面的Emitter的frame

emitter.emitterPosition = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
emitter.emitterSize = rect.size

代碼設(shè)置了Emitter中心點(diǎn)是layer的中心點(diǎn),size和layer一樣。

[]Creating an emitter cell

現(xiàn)在,您已經(jīng)配置了發(fā)射器的位置和大小,可以繼續(xù)添加Cell。 Cell是表示一個(gè)粒子源的數(shù)據(jù)模型。是CAEmitterLayer一個(gè)單獨(dú)的類,因?yàn)橐粋€(gè)發(fā)射器可以包含一個(gè)或多個(gè)粒子。 例如,在一個(gè)爆米花動(dòng)畫,你可以有三種不同的細(xì)胞代表一個(gè)爆米花的不同狀態(tài):完全炸開,一半炸開和沒有炸開:

let emitterCell = CAEmitterCell()
emitterCell.contents = UIImage(named: "flake.png")!.CGImage
emitterCell.birthRate = 20 
emitterCell.lifetime = 3.5 
emitter.emitterCells = [emitterCell]

代碼每一秒創(chuàng)建20個(gè)cell,每個(gè)cell有3.5s的生命周期,之后一些cell就會(huì)消失

[]Controlling your particles

上面設(shè)置的cell不會(huì)動(dòng),需要給它個(gè)加速度

emitterCell.xAcceleration = 10.0
emitterCell.yAcceleration = 70.0

設(shè)置Cell在X軸,Y軸的加速度。

emitterCell.velocity = 20.0 
// x-y平面的發(fā)射方向
// -M_PI_2 垂直向上
emitterCell.emissionLongitude = CGFloat(-M_PI_2)

設(shè)置起始速度,發(fā)射的方向是通過emissionLongitude屬性定義的。

[]Adding randomness to your particles

emitterCell.velocityRange = 200.0

設(shè)置其實(shí)速度的隨機(jī)范圍,每個(gè)粒子的速度將是一個(gè)隨機(jī)值之間(20-200)= 180 ~(20 + 200)= 220。負(fù)初始速度的粒子不會(huì)向上飛,一旦出現(xiàn)在屏幕上,他們就會(huì)開始浮動(dòng)。帶正速度的粒子先飛起來,然后再浮。

emitterCell.emissionRange = CGFloat(M_PI_2)

原來,你配置的所有粒子射直線上升(π/ 2角)作為他們的出現(xiàn)。上面這行代碼表示為每個(gè)粒子隨機(jī)選一個(gè)發(fā)射角度在(-π/2 + π/2)= 180度(-π/ 2 +π/2)= 0度之間。

[]Changing particle color

設(shè)置你的粒子顏色

emitterCell.color = UIColor(red: 0.9, green: 1.0, blue: 1.0, alpha: 1.0).CGColor

還可以設(shè)置粒子的顏色RGB范圍:

emitterCell.redRange = 0.1
emitterCell.greenRange = 0.1
emitterCell.blueRange = 0.1

由于RGB最大為1.0,所以red是取值0.81.0,green:0.91.0,blue:0.9~1.0

[]Randomizing particle appearance

之前的粒子都是一樣大的,這里給粒子分配一個(gè)隨機(jī)大小。

emitterCell.scale = 0.8 
emitterCell.scaleRange = 0.8

設(shè)置粒子是原來的80%大小,隨機(jī)范圍是從0.0到1.6。

emitterCell.scaleSpeed = -0.15

粒子每秒鐘按15%的體積縮小。
還可以設(shè)置透明度

emitterCell.alphaRange = 0.75 
emitterCell.alphaSpeed = -0.15

透明度 0.25~1.0,每秒透明度減少15%。

相關(guān)文章

最新評(píng)論