ios 如何设置 UIView 的原点参考?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10012406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:43:05  来源:igfitidea点击:

How to set a UIView's origin reference?

iosxcodecocoauiview

提问by Daniel

I am creating a UIImageView and adding it in a loop to my view, I set the initial frame to 0,0,1,47 and each passage of the loop I change the center of the image view to space them out.

我正在创建一个 UIImageView 并将其循环添加到我的视图中,我将初始帧设置为 0,0,1,47 并且循环的每个段落我更改图像视图的中心以将它们隔开。

I am always using 0 as the origin.y

我总是使用 0 作为 origin.y

The problem is the origin reference is in the centre of the image view, assuming we was in interface builder, this is equivalent to the image below.

问题是原点引用在图像视图的中心,假设我们在界面构建器中,这相当于下图。

How can I change the reference point in code ?

如何更改代码中的参考点?

enter image description here

在此处输入图片说明

回答by Tricertops

After reading these answers and your comments I'm not really sure what is your point.

阅读这些答案和您的评论后,我不确定您的观点是什么。

With UIViewyou can set position by 2 ways:

随着UIView您可以通过2种方式设置的位置:

  • center– It definitely says it is the center.
  • frame.origin– Top left corner, can't be set directly.
  • center- 它肯定说它是中心。
  • frame.origin– 左上角,不能直接设置。

If you want the bottom left corner to be at x=300, y=300 you can just do this:

如果您希望左下角位于 x=300, y=300 处,您可以这样做:

UIView *view = ...
CGRect frame = view.frame;
frame.origin.x = 300 - frame.size.width;
frame.origin.y = 300 - frame.size.height;
view.frame = frame;


But if you go one level deeper to magical world of CALayers(don' forget to import QuartzCore), you are more powerful.

但是如果你更深入一层CALayers(不要忘记导入 QuartzCore)的魔法世界,你就会更强大。

CALayerhas these:

CALayer有这些:

  • position– You see, it don't explicitely says 'center', so it may not be center!
  • anchorPointCGPointwith values in range 0..1 (including) that specifies point insidethe view. Default is x=0.5, y=0.5 which means 'center' (and -[UIView center]assumes this value). You may set it to any other value and the positionproperty will be applied to that point.
  • position– 你看,它没有明确地说“中心”,所以它可能不是中心!
  • anchorPointCGPoint值在 0..1(包括)范围内,指定视图的点。默认值为 x=0.5,y=0.5,这意味着“中心”(并-[UIView center]假定此值)。您可以将其设置为任何其他值,并且该position属性将应用于该点。

Example time:

示例时间:

  1. You have a view with size 100x100
  2. view.layer.anchorPoint = CGPointMake(1, 1);
  3. view.layer.position = CGPointMake(300, 300);
  4. Top left corner of the view is at x=200, y=200 and its bottom right corner is at x=300, y=300.
  1. 您有一个大小为 100x100 的视图
  2. view.layer.anchorPoint = CGPointMake(1, 1);
  3. view.layer.position = CGPointMake(300, 300);
  4. 视图的左上角在 x=200, y=200 处,右下角在 x=300, y=300 处。

Note:When you rotate the layer/view it will be rotated around the anchorPoint, that is the center by default.

注意:当您旋转图层/视图时,它将围绕 旋转anchorPoint,这是默认情况下的中心。

Bu since you just ask HOWto do specific thing and not WHATyou want to achieve, I can't help you any further now.

卜,因为你刚才问如何做具体的事情,而不是什么你想达到的,我不能再现在帮你任何。

回答by Phillip Mills

The object's frame includes its position in its superview. You can change it with something like:

对象的框架包括其在其父视图中的位置。您可以使用以下内容更改它:

CGRect frame = self.imageView.frame;
frame.origin.y = 0.0f;
self.imageView.frame = frame;

回答by jmstone617

If I am understanding you correctly, you need to set the frame of the image view you are interested in moving. This can be done in the simple case like this:

如果我理解正确,您需要设置您有兴趣移动的图像视图的框架。这可以在这样的简单情况下完成:

_theImageView.frame = CGRectMake(x, y, width, height);

Obviously you need to set x, y, width, and height yourself. Please also be aware that a view's frame is in reference to its parent view. So, if you have a view that is in the top left corner (x = 0, y = 0), and is 320 points wide and 400 points tall, and you set the frame of the image view to be (10, 50, 100, 50) and then add it as a subview of the previous view, it will sit at x = 10, y = 50 of the parent view's coordinate space, even though the bounds of the image view are x = 0, y = 0. Bounds are in reference to the view itself, frame is in reference to the parent.

显然,您需要自己设置 x、y、宽度和高度。另请注意,视图的框架是参考其父视图。因此,如果您的视图位于左上角 (x = 0, y = 0),宽 320 点,高 400 点,并且您将图像视图的框架设置为 (10, 50, 100, 50) 然后将其添加为上一个视图的子视图,它将位于父视图坐标空间的 x = 10, y = 50,即使图像视图的边界是 x = 0, y = 0 .bounds 是参考视图本身,frame 是参考父级的。

So, in your scenario, your code might look something like the following:

因此,在您的场景中,您的代码可能如下所示:

CGRect currentFrame = _theImageView.frame;
currentFrame.origin.x = 0;
currentFrame.origin.y = 0;
_theImageView.frame = currentFrame;

[_parentView addSubview:_theImageView];

Alternatively, you can say:

或者,你可以说:

CGRect currentFrame = _theImageView.frame;
_theImageView.frame = CGRectMake(0, 0, currentFrame.size.width, currentFrame.size.height);

[_parentView addSubview:_theImageView];

Either approach will set the image view to the top left of the parent you add it to.

任何一种方法都会将图像视图设置到您添加到的父级的左上角。