Core Animation学习笔记—第四节Building a Layer Hierarchy

各位iOS开发大佬们好:
我是一名swift+swiftUI栈的iOS小白,目前还在上大三,最近准备实习,面试的过程中发现现在大公司很多还在用OC + UIKit的技术栈,OC我还在考虑要不要学,目前想先把UIKit学完,这是我在官网学习UIKit英文文档时摘录的本人认为的重点,如果你们也觉得对你们有用的话欢迎持续关注,我大概一天更一节,有事除外。格式什么的我也就不做了,翻译都是我自己翻译的,哪里不对欢迎在评论区指正,感谢各位大佬支持

今天2021年12月4日
这一章叫做Building a Layer Hierarchy

However, a layer-backed view can act as the parent for standalone layers you create yourself
一个底层有layer的视图可以当作你自己创建的layer的父layer
以下是添加,删除,插入,修改layer的方法

Layer层级和view层级一样,子layer依赖于父layer的坐标,内容等并随着父layer的改变而改变
Always use integral numbers for the width and height of your layer.
layer的bounds最好用整形量
有些layer的属性会影响到其子layer的动画效果,比如speed,rotation等等

Using Constraints to Manage Your Layer Hierarchies in OS X
Constraints let you specify the position and size of a layer using a set of detailed relationships between the layer and its superlayer or sibling layers. Defining constraints requires the following steps:
Create one or more CAConstraint objects. Use those objects to define the constraint parameters.
Add your constraint objects to the layer whose attributes they modify.
Retrieve the shared CAConstraintLayoutManager object and assign to the immediate superlayer.
Figure 4-1 shows the attributes that you can use to define a constraint and the aspect of the layer that they impact. You can use constraints to change the position the layer based on the position of its edges of midpoints relative to another layer. You can also use them to change the size of the layer. The changes you make can be proportional to the superlayer or relative to another layer. You can even add a scaling factor or constant to the resulting change. This extra flexibility makes it possible to control a layer’s size and position very precisely using a simple set of rules.
Figure 4-1 Constraint layout manager attributes
在这里插入图片描述

Each constraint object encapsulates one geometry relationship between two layers along the same axis. A maximum of two constraint objects may be assigned to each axis and it is those two constraints that determine which attribute is changeable. For example, if you specify constraints for the left and right edge of the layer, the size of the layer changes. If you specify constraints for the left edge and width of the layer, the location of the layer’s right edge changes. If you specify a single constraint for one of the layer’s edges, Core Animation creates an implicit constraint that keeps the size of the layer fixed in the given dimension.
When creating constraints, you must always specify three pieces of information:
The aspect of the layer that you want to constrain
The layer to use as a reference
The aspect of the reference layer to use in the comparison
通过定义一个关系集合来约束layer的位置和尺寸,三步定义一个约束
创建一个CAConstraint对象并且设置约束参数
添加到需要修改属性的layer上
取出共享的CAConstraintLayoutManager 对象,赋给父layer
每个约束对象都包含了两个layer(可能为父子可能为同级)在同一坐标轴下的几何关系,比如如果约束了左边缘和宽度,那么右边缘将会发生改变以适应宽度(因为左边缘已经固定)
创建约束时必须明确的三点
需要约束的layer的方面
约束所参考的layer(参照物)
参照的layer需要对比的方面

To apply constraints at runtime, you must attach the shared CAConstraintLayoutManager object to the immediate superlayer. Each layer is responsible for managing the layout of its sublayers. Assigning the layout manager to the parent tells Core Animation to apply the constraints defined by its children. The layout manager object applies the constraints automatically. After assigning it to the parent layer, you do not have to tell it to update the layout.
通过添加共享的CAConstraintLayoutManager对象到父layer上在运行时使用约束,每个layer都有管理自己子layer的责任
With autoresizing rules, you designate whether the edges of your layer should remain at a fixed or variable distance from the corresponding edges of the superlayer. You can similarly designate whether the width or height of your layer is fixed or variable. The relationships are always between the layer and its superlayer. You cannot use autoresizing rules to specify relationships between sibling layers.
To set up the autosizing rules for a layer, you must assign the appropriate constants to the autoresizingMask property of the layer. By default, layers are configured to have a fixed width and height. During layout, the precise size and position of the layer is computed for you automatically by Core Animation and involve a complex set of calculations based on many factors. Core Animation applies the autoresizing behaviors before it asks your delegate to do any manual layout updates, so you can use the delegate to tweak the results of the autoresizing layout as needed.
Autoresizing仅用于父子关系,不用于同级关系,可以指定宽度,高度和边缘是否可变
通过为autoresizingMask 属性赋值来设置自动调整大小的规则,默认为固定的高度和宽度,可以通过代理修改自动调整大小的结果
在父layer的代理对象中实现layoutSublayersOfLayer:方法来手动调整布局,执行必要的布局计算(位置,尺寸等等)

Converting Coordinate Values Between Layers
Occasionally, you might need to convert a coordinate value in one layer to a coordinate value at the same screen location in a different layer. The CALayer class provides a set of simple conversion routines that you can use for this purpose:
convertPoint:fromLayer:
convertPoint:toLayer:
convertRect:fromLayer:
convertRect:toLayer:
In addition to converting point and rectangle values, you can also convert time values between layers using the convertTime:fromLayer: and convertTime:toLayer: methods. Each layer defines its own local time space and uses that time space to synchronize the beginning and ending of animations with the rest of the system. These time spaces are synchronized by default; however, if you change the animation speed for one set of layers, the time space for those layers changes accordingly. You can use the time conversion methods to to account for any such factors and ensure that the timing of both layers is synchronized.

这部分没啥翻译的,layer之间转换坐标和时间,转换时间为了对动画进行一些操作

猜你喜欢

转载自blog.csdn.net/Programmer_Roy/article/details/121702867