原文
Layers come with a full set of methods for reading and manipulating the layer hierarchy, parallel to the methods for reading and manipulating the view hierarchy. A layer has a superlayer property and a sublayers property, and these methods:
• addSublayer:
• insertSublayer:atIndex:
• insertSublayer:below:,insertSublayer:above: • replaceSublayer:with:
• removeFromSuperlayer
Unlike a view’s subviews property, a layer’s sublayers property is writable; thus, you can give a layer multiple sublayers in a single move, by assigning to its sublayers property. To remove all of a layer’s sublayers, set its sublayers property to nil.
Although a layer’s sublayers have an order, reflected in the sublayers order and regu‐ lated with the methods I’ve just mentioned, this is not necessarily the same as their back- to-front drawing order. By default, it is, but a layer also has a zPosition property, a CGFloat, and this also determines drawing order. The rule is that all sublayers with the same zPosition are drawn in the order they are listed among their sublayers siblings, but lower zPosition siblings are drawn before higher zPosition siblings. (The default zPosition is 0.)
Sometimes, the zPosition property is a more convenient way of dictating drawing order than sibling order is. For example, if layers represent playing cards laid out in a solitaire game, it will likely be a lot easier and more flexible to determine how the cards overlap by setting their zPosition than by rearranging their sibling order. Moreover, a subview’s layer is itself just a layer, so you can rearrange the drawing order of subviews by setting the zPosition of their underlying layers. In our code constructing Figure 3-5, if we assign the image view’s underlying layer a zPosition of 1, it is drawn in front of the red (left) rectangle:
[mainview addSubview:iv];
iv.layer.zPosition = 1;
Methods are also provided for converting between the coordinate systems of layers within the same layer hierarchy:
convertPoint:fromLayer:,convertPoint:toLayer:
convertRect:fromLayer:,convertRect:toLayer:
翻译
layers 伴随着一堆方法用来读取和操作layer的结构。和读取以及操作View的方法平行。一个layer有他的父layer和他所有的子layer的所有属性和方法。
addSublayer:
- insertSublayer:atIndex:
- insertSublayer:below:,insertSublayer:above:
- replaceSublayer:with:
- removeFromSuperlayer
不同于子View的属性,一个layer的子layer的属性是可写的。所以你可以给layer的多个子layer一些简单的操作通过分配子layer的属性。去清空所有子layer,设置子layer的属性为nil。
虽然一个layer的子layer有一个顺序,反映在子layer的顺序和我刚才提到的方法的调节。这不是必须和从后面到前面的绘制顺序一样。默认情况是这样的,但是一个layer同样有一个zPositon的属性,他是一个CGFloat类型,他同样确定了绘制的顺序。他的规则是具有相同zPosition属性的按照他们的兄弟视图列出的顺序绘制,但是比他们zPosition低的在高的视图前面。(他们的默认值是0)
有时候,zPosition属性是一个更加方便的方法相比兄弟排序。比如,如果想表现出打牌游戏摊开牌的情景,它将更加容易和灵活的确定牌应该怎么覆盖,通过设置他们的zPosition相对于重新设置他们的兄弟顺序。此外,一个子View的layer本身就是一个layer,所以你可以重新排列子view的绘制顺序通过设置他们的基本layer的zPosition。在我们的代码中如Figure 3-5,如果我们分配一个imageView 的基本zPosition为1,他将被画在前面。
UIImageView* iv =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"boy"]];
CGRect r = iv.frame;
r.origin = CGPointMake(180,180);
iv.frame = r;
[self.view addSubview:iv];
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(170, 170, 90, 90)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
iv.layer.zPosition = 1;
- convertPoint:fromLayer:
- convertPoint:toLayer:
- convertRect:fromLayer:
- convertRect:toLayer:
这四个方法试了,发现位置没有多大的变化。不知道具体该怎么用?
后在stackflow看到别人的回答
Every UIView has its own coordinates system. So if you have a UIView_1 that contains another UIView_2, they both have a point (10,10) within them.
- 每一个UIView都有一个自己的坐标定位,所以如果你有一个UIView_1包含了另一个UIView_2,他们都有内部坐标(10,10)。
convertPoint:toView: allows the developer to take a point in one view and convert the point to another view coordinate system. - 让开发者把当前视图的点移动到另一个视图的坐标系当中。
Example: view1 contains view2. The top left corner of view2 is located at view1 point (10,10), or better to say view2.frame.orgin = {10,10}. That {10,10} is based in the view1 coordinate system. So far so good. - 例如:view1 包含了View2.View2左上角的坐标在View1的(10,10)坐标点上.
The user touches the view2 at point {20,20} inside the view2. Now those coordinates are in the view2 coordinate system. You can now use covertPoint:toView: to convert {20,20} into the coordinate system of view1. touchPoint = {20,20} - 用户点击坐标点View2{20,20},现在他的坐标点在view2的坐标系中。你可以用covertPoint:toView。去改变作为View1坐标系中的{20,20}。
CGPoint pointInView1Coords = [view2 convertPoint:touchPoint toView:view1];
So now pointInView1Coords should be {30,30} in the view1 coordinate systems. Now that was just simple math on this example, but there are all sorts of things that contribute to the conversion. Transforms and scaling come to mind.
CGPoint pointInView1Coords = [view2 convertPoint:touchPoint toView:view1];
- 所以pointInView1Coords的坐标在View1中变成了{30,30},现在这只是一个简单的数学例子,但是这是所有的转换。
Read about UIView frame, bounds, and center. They are all related and they deal with coordinate systems for a view. Its confusing until you start doing stuff with them. Remember this frame and center are in the parent's coordinate system. Bounds is in the view's coordinate system.
阅读关于UIView的frame,bounds,和Center。他们都关系到处理这个View的坐标。他使人困惑直到你开始使用他们。记住frame和Center 在他们父视图的坐标中。bounds体现在她自己的坐标系中。
John
小结
** 看了之后才知道这不是用来改变View的而是用来计算坐标点距离的。果然code比文字能更好的说明问题。**