照例我們先看看效果圖
怎么樣?效果很不錯吧,下面來一起看看實現的過程和代碼示例。
實現原理
從圖中可以大致看出,爆炸點點都是取的某坐標的顏色值,然后根據一些動畫效果來完成的。
取色值
怎么取的view
的某個點的顏色值呢?google一下,就可以找到很多答案。就不具體說了。創建1*1的位圖,然后渲染到屏幕上,然后得到rgba。我這里寫的是uiview
的extension
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
extension uiview { public func colorofpoint(point:cgpoint) -> uicolor { var pixel:[cunsignedchar] = [0,0,0,0] let colorspace = cgcolorspacecreatedevicergb() let bitmapinfo = cgbitmapinfo(rawvalue: cgimagealphainfo.premultipliedlast.rawvalue) let context = cgbitmapcontextcreate(&pixel, 1, 1, 8, 4, colorspace, bitmapinfo.rawvalue) cgcontexttranslatectm(context, -point.x, -point.y) self.layer.renderincontext(context!) let red: cgfloat = cgfloat(pixel[0]) / 255.0 let green: cgfloat = cgfloat(pixel[1]) / 255.0 let blue: cgfloat = cgfloat(pixel[2]) / 255.0 let alpha: cgfloat = cgfloat(pixel[3]) / 255.0 return uicolor(red:red, green: green, blue:blue, alpha:alpha) } } |
粒子的生成
這里我寫的比較簡單,就是固定每個粒子大小,根據view的寬高算出橫向,縱向的粒子數,取該點的色值,設置粒子背景色,然后生成即可。
主要代碼如下:
framedict
是我預先計算好的坐標表,colordict
是顏色表。以"i-j"為key
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class func createexplosionpoints(containerlayer: explosionlayer, targetview: uiview, animationtype: explosionanimationtype) { let hcount = self.caculatepointhcount(containerlayer.targetsize.width) let vcount = self.caculatepointvcount(containerlayer.targetsize.height) for i in 0..<hcount { for j in 0..<vcount { let key = string(format: "%d-%d" , i, j) if let rect = containerlayer.framedict[key], color = containerlayer.colordict[key] { let layer = createexplosionpointlayer(rect, bgcolor: color, targetviewsize: containerlayer.targetsize) // animation layer.explosionanimation = self.createanimationwithtype(animationtype, position: layer.position, targetviewsize: containerlayer.targetsize) containerlayer.addsublayer(layer) layer.beginanimation() } } } } |
動畫效果
每個粒子都有一個caanimation
動畫,數據由調用者提供,靈活點。
這里定義了一個protocol:explosionanimationprotocol
,可以自定義實現了該protocol的動畫對象,提供動畫效果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
protocol explosionanimationprotocol { // 粒子初始位置 var oldposition: cgpoint { set get } // 粒子最終位置 var newposition: cgpoint { set get } // 縮放 var scale: cgfloat { set get } // 動畫時長 var duration: cftimeinterval { set get } // 動畫重復次數 var repeatcount: float { set get } // 生成動畫 func animation() -> caanimation // 設置動畫完之后的屬性 func resetlayerproperty(layer: calayer) } |
要發生爆炸view
的動畫效果
這個比較簡單,就是上下左右震動下。具體代碼就不貼出來了。
1
2
|
let shakeanimation = cakeyframeanimation(keypath: "position" ) ... |
代碼結構
大致思路就是這樣。代碼結構如下:
explosionlayer
是粒子的父容器,
explosionpointlayer
是粒子本身
explosionhelper
是個輔助類,用于計算粒子位置,顏色值。
fallanimation
,upanimation
是實現了explosionanimationprotocol
的動畫,分別提供向下落,向上的效果。
碰到的問題
剛開始我是在邊計算顏色值,邊繪制粒子,發現會卡一下才會有爆炸效果出來,分析可能是在計算顏色值在主線程,時間較長,所以卡住了。
后來想到放到后臺線程中去做,但是在主線程中取色值的時候,后臺必須執行完,所以用了信號量來進行同步。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// 震動效果 private func shake() { self.createsemaphore() // 計算位置,色值 self.caculate() let shakeanimation = cakeyframeanimation(keypath: "position" ) shakeanimation.values = [nsvalue.init(cgpoint: self.position), nsvalue.init(cgpoint: cgpointmake(self.position.x, self.position.y + 1)), nsvalue.init(cgpoint: cgpointmake(self.position.x + 1, self.position.y - 1)), nsvalue.init(cgpoint: cgpointmake(self.position.x - 1, self.position.y + 1))] shakeanimation.duration = 0.2 shakeanimation.repeatcount = 15 shakeanimation.delegate = self shakeanimation.removedoncompletion = true self.targetview?.layer.addanimation(shakeanimation, forkey: "shake" ) } |
當要爆炸的view開始震動時,就開始在后臺計算。震動動畫結束后,等待計算完成。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
override func animationdidstop(anim: caanimation, finished flag: bool ) { // wait for caculate dispatch_semaphore_wait(self.semaphore!, dispatch_time_forever) print( "shake animation stop" ) // begin explode if let targetview = self.targetview { self.parentlayer?.addsublayer(self) explosionhelper.createexplosionpoints(self, targetview: targetview, animationtype: self.animationtype) self.targetview?.hidden = true } } |
在后續的創建粒子時,就直接從緩存中取就行了。
總結
好了,以上就是ios實現爆炸效果的全部內容了,實現后的效果是不是非常的炫酷?感興趣的朋友們快快實踐起來吧,只有自己操作了才能真正的理解,希望這篇文章對大家的學習或者工作能帶來一定的幫助。