做 iOS 動畫與客製繪製,遲早會碰到 CALayer。它看似只是 UIView 背後的一個屬性,但一旦你要處理錨點、旋轉、3D 變換,就必須把 position、anchorPoint、bounds、frame 之間的關係搞透。本文就從繼承結構講到座標空間。
先看繼承鏈:UIView → UIResponder → NSObject,而 CALayer 直接繼承自 NSObject。在 NSObject 的樹狀結構裡,CALayer 的層級比 UIView 高。不過這件事本身說明不了什麼,我們直接看 API 文件的定義。
The CALayer class manages image-based content and allows you to perform animations on that content. Layers are often used to provide the backing store for views but can also be used without a view to display content. A layer's main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow. In addition to managing visual content, the layer also maintains information about the geometry of its content (such as its position, size, and transform) that is used to present that content onscreen. Modifying the properties of the layer is how you initiate animations on the layer's content or geometry. A layer object encapsulates the duration and pacing of a layer and its animations by adopting the CAMediaTiming protocol, which defines the layer's timing information. If the layer object was created by a view, the view typically assigns itself as the layer's delegate automatically, and you should not change that relationship. For layers you create yourself, you can assign a delegate object and use that object to provide the contents of the layer dynamically and perform other tasks. A layer may also have a layout manager object (assigned to the layoutManager property) to manage the layout of subviews separately.
大致意思是:CALayer 管理以圖像為基礎的內容,並允許對內容執行動畫。它常被用來作為 view 的底層繪製儲存區,但也可以脫離 view 獨立顯示內容。圖層本身具備視覺屬性可設定,例如背景色、邊框與陰影;同時維護內容的幾何資訊(位置、大小、變換),供螢幕呈現使用。修改圖層屬性,就是啟動動畫的方式。圖層透過遵循 CAMediaTiming 協議來封裝動畫的持續時間與節奏。
若圖層是由 view 建立的,view 會自動把自己設為圖層的 delegate,這層關係不該更動。若是你自己建立的圖層,則可以指定 delegate 物件,由它動態提供內容。圖層也可以有一個 layoutManager 來獨立管理子圖層的版面配置。
理解到這裡就夠了,接著記住一個關鍵事實:每個 UIView 內部預設都關聯著一個 CALayer。
UIView 有 frame、bounds、center 三個屬性;CALayer 則有 frame、bounds、position、anchorPoint。差別就在最後一項——anchorPoint,而它正是絕大多數困惑的來源。
@property CGPoint position;
@property CGPoint anchorPoint;
把一張 A4 白紙用圖釘釘在書桌上。如果釘得夠鬆,白紙就能繞著圖釘順時針或逆時針旋轉——這時候圖釘扮演的就是支點。
anchorPoint 就是那個圖釘,它的作用是作為變換的支點。旋轉是一種變換,平移與縮放也是。
很明顯,圖釘釘的位置不同,白紙的旋轉形態就完全不同:釘在正中央,紙會原地打轉;釘在左上角,紙會繞著角落甩出去。這完全由 anchorPoint 的位置決定。
在 iOS 中,anchorPoint 是相對於 bounds 的比例值,定義在單元座標空間(也就是左手座標系)中:
(0, 0)(0.5, 0.5)(預設值)(1, 0)(0, 1)(1, 1)注意這裡的比例是相對 layer 自身的 bounds,而不是相對螢幕。
The layer's position in its superlayer's coordinate space.
也就是說,position 是 layer 相對於 superlayer 座標空間的位置。
再看 API 對 frame 的兩句描述:
Layers have an implicit frame that is a function of the position, bounds, anchorPoint, and transform properties.
When you specify the frame of a layer, position is set relative to the anchor point. When you specify the position of the layer, bounds is set relative to the anchor point.
翻譯過來:圖層的 frame 是隱含屬性,由 position、bounds、anchorPoint、transform 共同決定。設定 frame 時,position 會依 anchorPoint 反推出來;設定 position 時,bounds 會依 anchorPoint 反推出來。
一句話總結:position 是「子 layer 的 anchorPoint 在 superlayer 中的位置座標」。
因此 position 點是相對 superlayer 說的,anchorPoint 點是相對 layer 自己說的——兩者其實是不同座標空間中的同一個重合點。
而設定 frame 時,被反推的是 position 的數值;設定 position 時,被反推的是 bounds 的 origin(也就是 frame 的左上角)。
最典型的場景是做旋轉動畫。如果你希望一個 view 繞著自己的中心旋轉,用 transform 就好;但如果要它繞著左上角旋轉,有兩種做法:把 anchorPoint 改成 (0, 0),或者用 CAKeyframeAnimation 繞自訂支點。前者更簡潔,但要注意——改 anchorPoint 會導致 frame 改變,因為 position 沒動而 bounds 的原點被重新計算。若不想讓視覺位置跳動,改完 anchorPoint 後要記得把 position 補回原本的值。
這也是無數人踩過的坑:layer.anchorPoint = CGPointMake(0, 0); 之後 view 就「跑掉了」。原因正是上面第 4 條結論的延伸——anchorPoint 變了,frame 跟著變,而 position 沒變。