CALayer 的属性分析(几何及图层关系属性)

    xiaoxiao2021-03-25  93

    /** 先列举CALayer的属性(几何及图层关系属性) **/

    /** Geometry and layer hierarchy properties. **/ /* The bounds of the layer. Defaults to CGRectZero. Animatable. */ @property CGRect bounds; /* The position in the superlayer that the anchor point of the layer's * bounds rect is aligned to. Defaults to the zero point. Animatable. */ @property CGPoint position; /* The Z component of the layer's position in its superlayer. Defaults * to zero. Animatable. */ @property CGFloat zPosition; /* Defines the anchor point of the layer's bounds rect, as a point in * normalized layer coordinates - '(0, 0)' is the bottom left corner of * the bounds rect, '(1, 1)' is the top right corner. Defaults to * '(0.5, 0.5)', i.e. the center of the bounds rect. Animatable. */ @property CGPoint anchorPoint; /* The Z component of the layer's anchor point (i.e. reference point for * position and transform). Defaults to zero. Animatable. */ @property CGFloat anchorPointZ; /* A transform applied to the layer relative to the anchor point of its * bounds rect. Defaults to the identity transform. Animatable. */ @property CATransform3D transform; /* Unlike NSView, each Layer in the hierarchy has an implicit frame * rectangle, a function of the `position', `bounds', `anchorPoint', * and `transform' properties. When setting the frame the `position' * and `bounds.size' are changed to match the given frame. */ @property CGRect frame; /* When true the layer and its sublayers are not displayed. Defaults to * NO. Animatable. */ @property(getter=isHidden) BOOL hidden;  

    1.bounds

      为了描述子视图在父视图中的位置,需要一套坐标体系,layer.bounds.origin 属性提供了这套坐标体系,子视图的 layer.postion 即是用来描述子视图在父视图 bounds.origin 坐标系中的位置。(bounds.origin同父视图左上角是重合的)

    2.position 和 anchorPoint

      如1.中所说,子视图在父视图中的位置,是相对父视图的坐标系(左上角)决定的,即是 子视图的anchorPoint 与 父视图坐标系的关系,其值为position。

      而锚点用的是比例值,取值0~1,在子视图size范围内取,默认(0.5,0.5),例子如下图:

     如果如上图,position不变,改变锚点为(1.0,1.0)的话:

      So,  在父视图 bounds 确定后,子视图在父视图中的位置,是由 anchorPoint 和 position 共同确定的。

    3.frame

      有了bounds描述一个视图世界中,视图的大小及视图与坐标系的关系(位置);并且有了anchorPoint和position用于描述子视图与父视图世界坐标系的关系(位置)。

      我们其实还需要一个量,用来描述视图的大小及视图与在父视图世界中的位置,这就是frame:

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

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

      frame.size = bounds.size;

      为啥又有了这么个东西,太深层次我还没去看,我觉得可能是为了用起来方便吧。。。

    4.有关3D的几个属性,后续会补上

    转载请注明原文地址: https://ju.6miu.com/read-22155.html

    最新回复(0)