UIView的幾何屬性有 frame,bounds,center.
CALayer的幾何屬性有 frame,bounds,position,anchorPoint
UIView的frame是由bounds,center來共同決定的。CALaye的position和UIView的center是同一個東東。
frame代表了圖層的外部座標(也就是在父圖層上佔據的空間),bounds是內部座標({0, 0}通常是圖層的左上角),center和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向左上角移動了