CAlayer中position与anchorPoint的理解

anchorPoint

anchorPoint点(锚点)的值是用相对bounds的比例值来确定的. 例如(0,0), (1,1)分别表示左上角、右下角,依此类推.
锚点都是对于自身来讲的. 确定自身的锚点,通常用于做相对的tranform变换.当然也可以用来确定位置

position

position是layer中的anchorPoint点在superLayer中的位置坐标.

anchorPoint、position、frame之间的相对关系

  • frame中的X,Y表示sublayer左上角相对于supLayer的左上角的距离
  • position中的X,Y表示sublay锚点相对于supLayer的左上角的距离
  • anchorPoint中的X,Y表示锚点的x,y的相对距离比例值
当确定锚点,改变frame时, position的值为:

position.x = frame.origin.x + anchorPoint.x * bounds.size.width;
position.y = frame.origin.y + anchorPoint.y * bounds.size.height;

确定锚点, 改变position时, frame的值为:

frame.origin.x = position.x - anchorPoint.x * bounds.size.width;
frame.origin.y = position.y - anchorPoint.y * bounds.size.height;

改变锚点, frame的值变化为

frame.origin.x = - anchorPoint.x * bounds.size.width + position.x;
frame.origin.y = - anchorPoint.y * bounds.size.height +position.y;

影响关系
锚点改变, position不影响, frame变化
frame变化, 锚点不影响, position变化
position变化, 锚点不影响, frame变化

你可能感兴趣的:(CAlayer中position与anchorPoint的理解)