CALayer有兩個(gè)非常重要的屬性:position和anchorPoint
@property CGPoint position;
用來(lái)設(shè)置CALayer在父層中的位置
以父層的左上角為原點(diǎn)(0, 0)
@property CGPoint anchorPoint;
稱為“定位點(diǎn)”翰舌、“錨點(diǎn)”
決定著CALayer身上的哪個(gè)點(diǎn)會(huì)在position屬性所指的位置
以自己的左上角為原點(diǎn)(0, 0)
它的x长窄、y取值范圍都是0~1,默認(rèn)值為(0.5, 0.5)
我們先來(lái)談anchorPoint(錨點(diǎn))
它的X值和Y值最大為1最小為0
默認(rèn)的anchorPoint(錨點(diǎn))位置是(0.5,0.5)他嫡,白點(diǎn)就是錨點(diǎn)所在位置
此時(shí)的anchorPoint(錨點(diǎn))位置是(0.5,0)诸尽,白點(diǎn)就是錨點(diǎn)所在位置
此時(shí)的anchorPoint(錨點(diǎn))位置是(0.5,1),白點(diǎn)就是錨點(diǎn)所在位置
此時(shí)的anchorPoint(錨點(diǎn))位置是(1,1),白點(diǎn)就是錨點(diǎn)所在位置
就算寬、高樣式改變了窍蓝,X和Y值取值范圍還是0~1
此時(shí)的anchorPoint(錨點(diǎn))位置是(1,1),白點(diǎn)就是錨點(diǎn)所在位置
@property CGPoint anchorPoint;
position
用來(lái)設(shè)置CALayer在父層中的位置
以父層的左上角為原點(diǎn)(0, 0)
添加一個(gè)紫色圖層到藍(lán)色圖層上繁成,紫色圖層是藍(lán)色圖層的子圖層
假設(shè)紫色圖層的position是(50它抱,50)
紫色圖層的anchorPoint(錨點(diǎn))位置是(0,0)
紫色圖層顯示到什么位置,由position屬性決定
圖一是原始圖
圖二是即將移動(dòng)的圖
圖三是移動(dòng)完成的圖
紫色圖層自身的anchorPoint(錨點(diǎn))必須和自身的position重合
假設(shè)紫色圖層的position是(50朴艰,50)
紫色圖層的anchorPoint(錨點(diǎn))位置是(0.5,0.5)
圖一是原始圖
圖二是即將移動(dòng)的圖
圖三是移動(dòng)完成的圖
假設(shè)紫色圖層的position是(50,50)
紫色圖層的anchorPoint(錨點(diǎn))位置是(1,1)
圖一是原始圖
圖二是即將移動(dòng)的圖
圖三是移動(dòng)完成的圖
紫色圖層寬高為100
父控件position默認(rèn)位置是左上角原點(diǎn)(0, 0)
紫色圖層的anchorPoint(錨點(diǎn))默認(rèn)位置是(0.5,0.5)
紫色圖層自身的anchorPoint(錨點(diǎn))必須和自身的position重合
如圖所示:所以只顯示四分之一出來(lái)
如果設(shè)置紫色圖層的anchorPoint(錨點(diǎn))為(0,0)
父控件position默認(rèn)位置是左上角原點(diǎn)(0, 0)
紫色圖層自身的anchorPoint(錨點(diǎn))必須和自身的position重合
如圖所示:顯示全部紫色圖層
只要是CALayer這種類型,他的anchorPoint默認(rèn)都是(0.5,0.5)