您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 爆炸粒子效果的實作範例代碼

編輯:iOS 開發綜合

照例我們先看看效果圖

如何?效果很不錯吧,下面就一起看看實作過程與代碼範例。

實作原理

從圖中不難看出,爆炸的點點都是取某坐標的顏色值,再搭配一些動畫效果來完成的。

取色值

怎麼取得 view 某個點的顏色值呢?搜尋一下就能找到很多答案,這裡就不細說了。做法是建立 1*1 的位圖,渲染到螢幕上,再取得 RGBA。我這裡寫的是 UIView 的 extension。

 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。

 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 的動畫對象,提供動畫效果。

 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 的動畫效果

這個比較簡單,就是上下左右震動一下,具體代碼就不貼出來了。

 let shakeAnimation = CAKeyframeAnimation(keyPath: "position") ...

代碼結構

大致思路就是這樣,代碼結構如下:


    ExplosionLayer 是粒子的父容器,

    ExplosionPointLayer 是粒子本身

    ExplosionHelper 是個輔助類,用於計算粒子位置、顏色值。

    FallAnimation、UpAnimation 是實作了 ExplosionAnimationProtocol 的動畫,分別提供向下落、向上的效果。

碰到的問題

一開始我在邊計算顏色值、邊繪製粒子,結果發現會卡一下才出現爆炸效果。分析後推測是計算顏色值放在主線程、耗時較長所致。

後來想到把它放到後台線程去做,但在主線程取色值時,後台必須執行完成,於是用了信號量來做同步。

 // 震動效果 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 開始震動時,就同步在後台計算;震動動畫結束後,再等待計算完成。

 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 實現爆炸效果的全部內容。實作後的效果是不是非常炫酷?感興趣的朋友快快動手實踐吧,只有親自操作才能真正理解,希望這篇文章對大家的學習或工作能帶來幫助。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved