UIView 有三个属性 frame bounds center 我们都了解,这里就不多说了。
CALayer 也有对应的属性 frame bounds position anchorPoint
默认的anchorPoint为(0.5,0.5) 这时position就是CALayer的中心点 相当于UIView的center
anchorPoint相当于钉在墙上的白纸上的图钉 ,主要作用就是用来做图形变换的指定,如旋转 平移 缩放。它的坐标是相对于自身的比例坐标 x,y值最大是1,最小是0
position是CALayer在父Layer坐标系中的自身anchorPoint的位置,两者是重合的一点,只是position是相对于父Layer坐标 anchorPoint是相对于自身的比例坐标
两者之间的换算:
position.x = frame.origin.x + anchorPoint.x *
bounds.size.width;
position.y = frame.origin.y + anchorPoint.y *
bounds.size.height;
特别注意的是,两者之间互不影响 单个修改任一个的值 并不会影响两一个的值 只会影响frame.origin的值
三者之间的换算:
frame.origin.x = position.x - anchorPoint.x *
bounds.size.width;
frame.origin.y = position.y - anchorPoint.y *
bounds.size.height;
从公式中我们可以知道:当position.x增大时frame.origin.x也相应增加 而且增加的是一样的值,position.y也是一样道理
当增大时 frame.origin.x是减小的 减小的值=anchorPoint.x增加的幅度 * bounds.size.width。anchorPoint.y也是一样道理
个人博客地址:https://youyou0909.github.io