CLayer的相關知識

CALayer常用屬性

屬性 說明 是否支持隱式動畫
anchorPoint 中心點position重合的一個點,稱爲“錨點”,錨點的描述是相對於x、y位置比例而言的默認在圖像中心點(0.5,0.5)的位置
backgroundColor 圖層背景顏色
borderColor 邊框顏色
borderWidth 邊框寬度
bounds 圖層大小
contents 圖層顯示內容,例如可以將圖片作爲圖層內容顯示
contentsRect 圖層顯示內容的大小和位置
cornerRadius 圓角半徑
doubleSided 圖層背面是否顯示,默認爲YES
frame 圖層大小和位置,不支持隱式動畫,所以CALayer中很少使用frame,通常使用bounds和position代替
hidden 是否隱藏
mask 圖層蒙版
maskToBounds 子圖層是否剪切圖層邊界,默認爲NO
opacity 透明度 ,類似於UIView的alpha
position 圖層中心點位置,類似於UIView的center
shadowColor 陰影顏色
shadowOffset 陰影偏移量
shadowOpacity 陰影透明度,注意默認爲0,如果設置陰影必須設置此屬性
shadowPath 陰影的形狀
shadowRadius 陰影模糊半徑
sublayers 子圖層
sublayerTransform 子圖層形變
transform 圖層形變



  • 隱式屬性動畫的本質是這些屬性的變動默認隱含了CABasicAnimation動畫實現,詳情大家可以參照Xcode幫助文檔中“Animatable Properties”一節。
  • 在CALayer中很少使用frame屬性,因爲frame本身不支持動畫效果,通常使用bounds和position代替。
  • CALayer中透明度使用opacity表示而不是alpha;中心點使用position表示而不是center。
  • anchorPoint屬性是圖層的錨點,範圍在(0~1,0~1)表示在x、y軸的比例,這個點永遠可以同position(中心點)重合當圖層中心點固定後,調整anchorPoint即可達到調整圖層顯示位置的作用(因爲它永遠和position重合)

爲了進一步說明anchorPoint的作用,假設有一個層大小100*100,現在中心點位置(50,50),由此可以得出frame(0,0,100,100)。上面說過anchorPoint默認爲(0.5,0.5),同中心點position重合,此時使用圖形描述如圖1;當修改anchorPoint爲(0,0),此時錨點處於圖層左上角,但是中心點poition並不會改變,因此圖層會向右下角移動,如圖2;然後修改anchorPoint爲(1,1,),position還是保持位置不變,錨點處於圖層右下角,此時圖層如圖3。

anchorPoint


################################
對於圖一,我的理解是,首先根據bounds(0,0,0,100,100)確定layer的大小。然後設置position(50 , 50) 因爲,position相當於center所以,就確定了layer在父layer中的位置。同時anchorPoint默認的值是(0.5,0.5)。對於圖二和圖三,首先要明白一條,position的位置不再改變。要移動layer的位置,改變的是anchorPoint的值。同時要記住的ancorPoint是和position重合的。也就是說anchorPoint稱爲"定位點",它決定着CALayer身上的哪個點會在position屬性所指的位置

對於圖一來說,layer的中心點落在position的位置。
對於圖二來說,layer的左上角的點落在position的位置。
對於圖三來說,layer的右下角的點落在position的位置。

決定着CALayer身上的哪個點會在position所指的位置上。它的x、y取值範圍都是0~1,默認值爲(0.5, 0.5),因此,默認情況下,CALayer的中點會在position所指定的位置上。

###############################

怎麼position和anchorPoint來確定位置。

首先要清楚,UIView的frame屬性是由center和bounds屬性計算得到的。
frame.origin.x = center.x - bounds.size.width/2.0;
frame.origin.y = center.y - bounds.size.height/2.0;
frame.size = bounds.size;
相對的,身爲UIView下級結構的CALayer呢?
CALayer的position(相當於center),bounds,anchorPoint是什麼關係呢?
雖然沒有frame,但是CALayer的顯示(虛擬frame)也是由這些組件算出來的
frame.origin.x = position.x - anchorPoint.x * bounds.size.width/2.0;
frame.origin.y = position.y - anchorPoint.x * bounds.size.height/2.0;
frame.size = bounds.size;
所以,當我們在上面修改anchorPoint的時候,實際上修改了顯示的運算元素!這樣當anchorPoint修改爲(1.0,1.0)的時候,經過重新運算,CALayer向左上角移動了
===================================


參考:
http://blog.csdn.net/primer_programer/article/details/9904287
http://www.cnblogs.com/kenshincui/p/3972100.html



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章