frame、bounds CALayer的position 以及 anchorPoint

最近在复习oc的基础知识发现了很多不足特此温习记录笔记

一 frame
frame是一个结构体 在iOS上是左手坐标 原点在 左上角在mac上是右手坐标原点在左下角。

struct CGRect {
    CGPoint origin;
    CGSize size;
};

frame是相对于父视图的坐标(通俗讲 [A addSubview B ] A 即为父视图)

二 bounds
bounds 相对于其本身的坐标 那么以本身为坐标无论origin.x 和origin.y的值是多少他的位置都不会改变可以看做是以(0,0)为坐标原点的size大小的视图。

三 UIView 与 CALayer
UIView的继承结构:UIResponder : NSObject
CALyer的继承结构:NSObject

可见UIView 与CALyer 最本质的区别是 UIView 能响应事件而CALyer主要负责的是渲染绘制界面
CALyer是相较于UIView更底层的图形类 CALyer是对于底层API(OpenGL ES)的高度封装 将复杂的Api封装成简单的类 并且暴露出基础的几何属性 :位置、尺寸、图形变换等等
而UIView是基于CALyer的一层封装遵循CALyer的代理去实现一些功能
关于 CALayer 的三层树的解释
逻辑树 动画树 显示树 等后续理解后再做解释
四 CALayer的position以及anchorPoint
这个我看了好久 对于anchorPoint 迷糊的不行看了很多的文章最后发现他的作用是确定CALayer的旋转点。
anchorPoint position 都是CGPoint 类型 但是 anchorPoint 是一个区间的为 (0,0)~(1,1) 你可以理解为 这个点等于 (anchorPoint.x × bounds.width,anchorPoint.y × bounds.height) 默认为(0.5,0.5)

position 就是 anchorPoint这个点相对于父视图的位置坐标
这句话怎么理解举个例子:

假设有一个UIView 的 frame 为CGRectMake(100, 100, 100, 100)
那么这个view的CALayer的anchorPoint默认为(0.5,0.5) 这个位置相对于自身 位置是(50,50) 那相对于父视图(view.superVIew.layer)的位置就是(frame.origin.x + bouds.size.width×0.5 ,frame.origin.y + bouds.size.height×0.5) 那么position就是(150,150)

那么总结下
frame.origin.x = position.x - anchorPoint.x * bounds.size.width;
frame.origin.y = position.y - anchorPoint.y * bounds.size.height

1 frame 受 position 与 anchorPoint共同影响 也就是说单独改变 position 或者 anchorPoint 只会影响frame.origin 而不会影响彼此

2 anchorPoint 默认为 (0.5,0.5) 所以 不修改值的情况下 frame修改 只影响position

3,当修改anchorPoint 修改时 position 是不改变的 所以变相修改了 frame.origin 如果不想frame 位置相对改变 需要修改position 根据上面的公式进行计算

4 其实 anchorPoint 决定的是 layer的哪个点在position的位置 anchorPoint(0.0) 那就是左上角在 postion的位置上 anchorPoint(1.1)就是右下角 anchorPoint(0,1)就是左下角

5 anchorPoint 是旋转点

如果出现错误请指正 我会尽快修改内容

你可能感兴趣的:(frame、bounds CALayer的position 以及 anchorPoint)