在Core Animation中隆嗅, 借助Core Animation 提供的幾個動畫類我們可以對一個Layer做各種酷炫的動畫仍劈,在實例 CAPropertyAnimation 的時候( CABasicAnimation
和 CAKeyFrameAnimation
均繼承自該類),通過構(gòu)造方法 convenience init(keyPath path: String?)
,需要傳入一個字符串塘揣。 那么問題來了弥激,這個參數(shù)到底可以取哪些值呢?
我們知道 CALayer
實現(xiàn)了 NSKeyValueCoding
協(xié)議释牺,因此我們可以使用 KVC 對 CALayer的實例中的屬性進(jìn)行取值和賦值萝衩,而在 Core Animation 提供的幾個動畫類影響的對象便是 CALayer
的實例,我們可以推測在 CAPropertyAnimation 類中構(gòu)造方法 convenience init(keyPath path: String?)
的keyPath參數(shù)的取值 應(yīng)該是 CALayer 中的屬性没咙。 帶著這個推測猩谊,參考蘋果的官方文檔,筆者把可以用作keyPath的值列舉如下祭刚。
一牌捷、可動畫屬性
1. 幾何屬性 (Geometry Properties)
Field Key Path | Remark | En Description | |
---|---|---|---|
transform.rotation.x | 按x軸旋轉(zhuǎn)的弧度 | Set to an NSNumber object whose value is the rotation, in radians, in the x axis. | |
transform.rotation.y | 按y軸旋轉(zhuǎn)的弧度 | Set to an NSNumber object whose value is the rotation, in radians, in the y axis. | |
transform.rotation.z | 按z軸旋轉(zhuǎn)的弧度 | Set to an NSNumber object whose value is the rotation, in radians, in the z axis. | |
transform.rotation | 按z軸旋轉(zhuǎn)的弧度, 和transform.rotation.z效果一樣 | Set to an NSNumber object whose value is the rotation, in radians, in the z axis. This field is identical to setting the rotation.z field. | |
transform.scale.x | 在x軸按比例放大縮小 | Set to an NSNumber object whose value is the scale factor for the x axis. | |
transform.scale.y | 在x軸按比例放大縮小 | Set to an NSNumber object whose value is the scale factor for the y axis. | |
transform.scale.z | 在z軸按比例放大縮小 | Set to an NSNumber object whose value is the scale factor for the z axis. | |
transform.scale | 按比例放大縮小 | Set to an NSNumber object whose value is the average of all three scale factors. | |
transform.translation.x | 沿x軸平移 | Set to an NSNumber object whose value is the translation factor along the x axis. | |
transform.translation.y | 沿y軸平移 | Set to an NSNumber object whose value is the translation factor along the y axis. | |
transform.translation.z | 沿z軸平移 | Set to an NSNumber object whose value is the translation factor along the z axis. | |
transform.translation | x,y 坐標(biāo)均發(fā)生改變 | Set to an NSValue object containing an NSSize or CGSize data type. That data type indicates the amount to translate in the x and y axis. | |
transform | CATransform3D 4*4矩陣 | ||
bounds | layer大小 | ||
position | layer位置 | ||
不支持 frme 屬性 | computed from the bounds and position and is NOT animatable | ||
anchorPoint | 錨點位置 | ||
cornerRadius | 圓角大小 | ||
zPosition | z軸位置 |
2.背景屬性 (Background Properties)
Field Key Path | Remark | En Description | |
---|---|---|---|
backgroundColor | 背景顏色 |
3.Layer內(nèi)容 (Layer Content)
Field Key Path | Remark | En Description |
---|---|---|
contents | Layer內(nèi)容,呈現(xiàn)在背景顏色之上 | |
contentsRect | The rectangle, in the unit coordinate space, that defines the portion of the layer’s contents that should be used. | |
masksToBounds | setting the layer’s masksToBounds property to YES does cause the layer to clip to its corner radius |
4.子Layer內(nèi)容 (Sublayers Content)
Field Key Path | Remark | En Description |
---|---|---|
sublayers | 子Layer數(shù)組 | |
sublayerTransform | 子Layer的Transform | Specifies the transform to apply to sublayers when rendering. |
5.邊界屬性 (Border Attributes)
Field Key Path | Remark | En Description | |
---|---|---|---|
borderColor | |||
borderWidth |
6.陰影屬性 (Shadow Properties)
Field Key Path | Remark | En Description | |
---|---|---|---|
shadowColor | 陰影顏色 | ||
shadowOffset | 陰影偏移距離 | ||
shadowOpacity | 陰影透明度 | ||
shadowRadius | 陰影圓角 | ||
shadowPath | 陰影路徑 |
7.透明度 (Opacity Property)
Field Key Path | Remark | En Description | |
---|---|---|---|
opacity | 透明度 | ||
hiden |
8.遮罩 (Mask Properties)
Field Key Path | Remark | En Description | |
---|---|---|---|
mask |
9.ShapeLayer屬性 (ShapeLayer)
Field Key Path | Remark | En Description | |
---|---|---|---|
fillColor | |||
strokeColor | |||
strokeStart | 從無到有 | ||
strokeEnd | 從有到無 | ||
lineWidth | 路徑的線寬 | ||
miterLimit | 相交長度的最大值 | ||
lineDashPhase | 虛線樣式 |
二袁梗、注意
這里特別需要注意的是 layer的 frame
是不支持動畫的宜鸯,我們可以通過改變position
和````bounds``` 變通實現(xiàn)