很多精彩動效其實是好幾種動畫疊在一起的結果。有時候你只想要其中某一種,卻被龐大的代碼庫和長篇教程嚇退。這個系列做的事就是:把複雜動效裡不常見的那一小塊拆出來單獨講清楚——授人以魚不如授人以漁。

第一講的主角,是 BubbleTransition 裡最搶眼的形變轉場。它在 StarWars.iOS 的轉場動效裡出現過兩次,效果非常抓人;Raywenderlich.com 也曾經寫過教程復刻這種效果。

按慣例,這種由一個圓形不斷擴張、最終覆蓋全屏的過場,會用 UIBezierPath + CAShapeLayer + maskLayer 這套組合拳來做。但我翻了一下 BubbleTransition 的源碼,發現它的實現方式完全不同。

慢放下來看,秘密一目了然:作者只用了一個「組合視圖 + 縮放」的手法。就是這麼樸素的一招,撐起了那個看起來很炫的形變動畫。不得不說,寫這段代碼的人真是有才。
至於另外兩種做法,走的還是 UIBezierPath + CAShapeLayer + maskLayer 的老路。網上關於這套組合的介紹不少,也有人用它實現過不規則形狀的視圖——其實那種效果用上面「組合視圖」的土法子也能做,但一旦圖形變複雜,還是得靠貝塞爾曲線。
UIBezierPath:在 iOS 裡它不只是畫曲線的工具。矩形、圓形、圓角矩形、橢圓都能生成,是個通用性很強的圖形對象。
CAShapeLayer:CALayer 的子類,正如名字所示,配合 path 屬性可以做出很多常規手段難以實現的外形,還能實現線條生長一類的描邊動畫。
maskLayer:也就是 CALayer 的 mask 屬性,本身也是一個 CALayer;UIView 上則有對應的 maskView。它的效果有點像遊戲裡的「探索地圖」——沒被遮罩覆蓋的部分才會顯示出來。有意思的是,BubbleTransition 作者的手法本身就是 mask 思想的一種應用,只是用得更巧。
把同一個效果改用 UIBezierPath + CAShapeLayer + maskLayer 實現,慢放之後是這樣:

核心代碼並不長:
// 生成一大一小兩條橢圓路徑,ovalInRect 表示緊貼矩形內部的橢圓 // bubble 是 BubbleTransition 中加入的背景視圖 let circleMaskPathInitial = UIBezierPath(ovalInRect: bubble.frame) let finalRect = CGRectInset(bubble.frame, -1000, -1000) let circleMaskPathFinal = UIBezierPath(ovalInRect: finalRect) // 用 CAShapeLayer 作為 mask let maskLayer = CAShapeLayer() maskLayer.path = circleMaskPathFinal.CGPath presentedControllerView.layer.mask = maskLayer // 對 CAShapeLayer 的 path 屬性做動畫 let maskLayerAnimation = CABasicAnimation(keyPath: "path") maskLayerAnimation.fromValue = circleMaskPathInitial.CGPath maskLayerAnimation.toValue = circleMaskPathFinal.CGPath maskLayerAnimation.duration = self.transitionDuration(transitionContext) maskLayer.addAnimation(maskLayerAnimation, forKey: "path")
把遮罩路徑換成梅花形,效果如下。老實說這個形狀放在轉場裡並不好看,這裡純粹是為了示範「路徑可以是任意形狀」。

let width = presentedControllerView.frame.width
let height = presentedControllerView.frame.height
let circleMaskPathInitial = UIBezierPath()
circleMaskPathInitial.moveToPoint(CGPoint(x: 0, y: height))
circleMaskPathInitial.addArcWithCenter(CGPoint(x: width / 4, y: height), radius: width / 4,
startAngle: CGFloat(M_PI), endAngle: CGFloat(M_PI * 3 / 2), clockwise: true)
circleMaskPathInitial.addArcWithCenter(CGPoint(x: width / 2, y: height - width / 4), radius: width / 4,
startAngle: CGFloat(M_PI), endAngle: CGFloat(M_PI * 2), clockwise: true)
circleMaskPathInitial.addArcWithCenter(CGPoint(x: width * 3 / 4, y: height), radius: width / 4,
startAngle: CGFloat(M_PI * 3 / 2), endAngle: CGFloat(M_PI * 2), clockwise: true)
circleMaskPathInitial.moveToPoint(CGPoint(x: 0, y: height))
circleMaskPathInitial.closePath()
let circleMaskPathFinal = UIBezierPath()
circleMaskPathFinal.moveToPoint(CGPoint(x: -width, y: height))
circleMaskPathFinal.addArcWithCenter(CGPoint(x: 0, y: height), radius: height,
startAngle: CGFloat(M_PI), endAngle: CGFloat(M_PI * 3 / 2), clockwise: true)
circleMaskPathFinal.addArcWithCenter(CGPoint(x: width / 2, y: 0), radius: width / 2,
startAngle: CGFloat(M_PI), endAngle: CGFloat(M_PI * 2), clockwise: true)
circleMaskPathFinal.addArcWithCenter(CGPoint(x: width, y: height), radius: height,
startAngle: CGFloat(M_PI * 3 / 2), endAngle: CGFloat(M_PI * 2), clockwise: true)
circleMaskPathFinal.moveToPoint(CGPoint(x: -width, y: height))
circleMaskPathFinal.closePath()

類方法 bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise: 用來添加圓弧,clockwise 決定繪製方向:true 為順時針,false 為逆時針。下面這張是官方文檔裡的座標示意圖:

畫這類梅花形路徑時有一個坑要注意:起始段和收尾段的走向必須保持一致,否則動畫會出現奇怪的跳變或反轉。
繞了一圈差點搞成烏龍,但收穫是:現在我們手上有兩種實現這類形變遮罩的方式。
BubbleTransition 的手法實在令人佩服,代價低、效果卻幾乎不打折扣,很多原本要用 maskLayer 的場景都可以拿它替換。當然,面對更複雜的曲線邊界,還是老老實實用貝塞爾曲線更省心。
UIBezierPath + CAShapeLayer + maskLayer 這套組合特別適合做不規則視圖:曲線菜單、任務欄、波紋效果、注水視圖等等,其餘就留給你的想象力了。