CALayer 的 anchorPoint 與 position

CALayer 像UIView 同樣,是有層級的, 一個layer 能夠有superLayer( 即layer 所處的容器)。this

layer 和superLayer的相對位置的肯定須要layer的一個點和superLayer的一個點來肯定(不考慮的layer的transform, 默認layer是橫豎放置的),那麼這兩個點怎麼肯定呢?spa


layer的anchorPoint 就是來肯定layer上的鏈接點的(錨點), 使用的是layer本身的座標系,把長寬看做1, 而後按比例計算;orm

而 position 則是指定superLayer上的一個點,使用的superLayer的座標系。ci

而後使這兩個點鏈接(重合),這樣就肯定了layer在superLayer中的位置。it


能夠想象layer是船, superLayer是海底,position 指定了把錨落在海底的哪一點,anchorPoint指定了在船的哪一點拋下錨,這樣就肯定了船相對於海底的位置(假設船是橫向放置的)。 當position不變的時候,要改變anchorPoint的位置,那麼船必然要移動的;一樣的,當anchorPoint不變,要改變position的位置,船也是要移動的。io

另外,船若是轉動(transform),也只能圍繞着錨點。form


The value of this property  (position)  is specified in points and is always specified relative to the value in the anchorPoint property. For new standalone layers, the default position is set to (0.0, 0.0). Changing the frame property also updates the value in this property.容器

anchorPoint 默認是(0.5,0.5);當設置frame的時候,position也會被計算;transform

相關文章
相關標籤/搜索