在更改其anchorPoint之后设置其框架时,为什么视图会移动? [英] Why does my view move when I set its frame after changing its anchorPoint?

查看:68
本文介绍了在更改其anchorPoint之后设置其框架时,为什么视图会移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个UILabel实例,并将它们添加到我的ViewController视图中. 然后将每个anchorPointanchorPoint从0.5更改为1.0(x和y).

I made two instances of UILabel and added them to my ViewController's view. And then I changed the anchorPoint of each from 0.5 to 1.0 (x and y).

接下来,我将uiLabel2的帧重置为使用(100,100,100,20)创建的帧.

Next, I reset the frame of uiLabel2 to its frame I created it with: (100,100,100,20).

当我运行应用程序时,uiLabel1uiLabel2显示在不同的位置.为什么?

When I run the app, uiLabel1 and uiLabel2 show at different positions. Why?

UILabel *uiLabel1 = [[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 20)] autorelease];
uiLabel1.text = @"UILabel1";
uiLabel1.layer.anchorPoint = CGPointMake(1, 1);

UILabel *uiLabel2 = [[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 20)] autorelease];
uiLabel2.text = @"UILabel2";
uiLabel2.layer.anchorPoint = CGPointMake(1, 1);
uiLabel2.frame = CGRectMake(100, 100, 100, 20);

[self.view addSubview:uiLabel1];
[self.view addSubview:uiLabel2];

推荐答案

CALayer具有四个属性,这些属性决定了它在其上层中的显示位置:

A CALayer has four properties that determine where it appears in its superlayer:

  • position(与视图的center属性相同)
  • bounds(实际上仅是boundssize部分)
  • anchorPoint
  • transform
  • position (which is the same as the view's center property)
  • bounds (actually only the size part of bounds)
  • anchorPoint
  • transform

您会发现frame不是不是这些属性之一. frame属性实际上是从这些属性派生的.设置frame属性时,图层实际上会根据您提供的框架和图层现有的anchorPoint来更改其centerbounds.size.

You will notice that frame is not one of those properties. The frame property is actually derived from those properties. When you set the frame property, the layer actually changes its center and bounds.size based on the frame you provide and the layer's existing anchorPoint.

您创建第一层(通过创建第一个UILabel(它是UIView的子类,并且每个UIView都有一个层)),为其提供100,100,100,20的框架.该图层的默认锚点为0.5,0.5.因此,它将边界计算为0,0,100,20,将其位置计算为150,110.看起来像这样:

You create the first layer (by creating the first UILabel, which is a subclass of UIView, and every UIView has a layer), giving it a frame of 100,100,100,20. The layer has a default anchor point of 0.5,0.5. So it computes its bounds as 0,0,100,20 and its position as 150,110. It looks like this:

然后将其锚点更改为1,1.由于您不会直接更改图层的位置或边界,也不会通过设置其框架而间接更改它们,因此图层将移动,以使其新的锚点位于其上层的(不变)位置:

Then you change its anchor point to 1,1. Since you don't change the layer's position or bounds directly, and you don't change them indirectly by setting its frame, the layer moves so that its new anchor point is at its (unchanged) position in its superlayer:

如果现在请求图层(或视图)的框架,则会得到50,90,100,20.

If you ask for the layer's (or view's) frame now, you will get 50,90,100,20.

创建第二层(用于第二个UILabel)时,更改其锚点后,请设置其框架.因此,该图层会根据您提供的框架及其现有的锚点来计算新的位置和边界:

When you create the second layer (for the second UILabel), after changing its anchor point, you set its frame. So the layer computes a new position and bounds based on the frame you provide and its existing anchor point:

如果现在向图层(或视图)询问其框架,则会得到您设置的框架100,100,100,20.但是,如果您要求其位置(或视图的中心),则会得到200,120.

If you ask the layer (or view) for its frame now, you will get the frame you set, 100,100,100,20. But if you ask for its position (or the view's center), you will get 200,120.

这篇关于在更改其anchorPoint之后设置其框架时,为什么视图会移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆