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

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

问题描述

我创建了两个 UILabel 实例并将它们添加到我的 ViewController 的视图中.然后我将每个的 anchorPoint 从 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部分)
  • 锚点
  • 变换
  • position (which is the same as the view's center property)
  • bounds (actually only the size part of bounds)
  • anchorPoint
  • transform

您会注意到 frame 不是这些属性之一.frame 属性实际上是从这些属性派生的.当您设置 frame 属性时,图层实际上会根据您提供的框架和图层的现有 更改其 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.

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

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