21

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

我总是使用 0 作为 origin.y

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

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

在此处输入图像描述

4

4 回答 4

45

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

您可以通过UIView两种方式设置位置:

  • center- 它肯定说它是中心。
  • frame.origin– 左上角,不能直接设置。

如果您希望左下角位于 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;

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

CALayer有这些:

  • position– 你看,它没有明确地说“中心”,所以它可能不是中心!
  • anchorPointCGPoint值在 0..1(包括)范围内,指定视图内的点。默认值为 x=0.5, y=0.5,表示“中心”(并-[UIView center]假定该值)。您可以将其设置为任何其他值,并且该position属性将应用于该点。

示例时间:

  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。

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

但是,由于您只是问如何做具体的事情而不是想要实现的目标,所以我现在无法为您提供更多帮助。

于 2013-01-05T17:41:41.073 回答
8

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

CGRect frame = self.imageView.frame;
frame.origin.y = 0.0f;
self.imageView.frame = frame;
于 2012-04-04T13:56:02.363 回答
2

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

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

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

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

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

[_parentView addSubview:_theImageView];

或者,您可以说:

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

[_parentView addSubview:_theImageView];

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

于 2012-04-04T13:54:37.463 回答
0

我想我会在 Swift 中有所作为。

如果想要通过指定坐标到该视图在 X 和 Y 中的原点的坐标来设置屏幕上的视图位置,通过一些数学运算,我们可以计算出视图的中心需要在哪里以便要根据需要定位的框架的原点。

此扩展使用视图的框架来获取宽度和高度。

计算新中心的方程几乎是微不足道的。请参阅以下扩展名:

extension CGRect {
    
    // Created 12/16/2020 by Michael Kucinski for anyone to reuse as desired
    func getCenterWhichPlacesFrameOriginAtSpecified_X_and_Y_Coordinates(x_Position: CGFloat, y_Position: CGFloat)  -> CGPoint
    {
        // self is the CGRect
        let widthDividedBy2 = self.width / 2
        let heightDividedBy2 = self.height / 2
        
        // Calculate where the center needs to be to place the origin at the specified x and y position
        
        let desiredCenter_X = x_Position + widthDividedBy2
        let desiredCenter_Y = y_Position + heightDividedBy2
        
        let calculatedCenter : CGPoint = CGPoint(x: desiredCenter_X, y: desiredCenter_Y)
        
        return calculatedCenter // Using this point as the center will place the origin at the specified X and Y coordinates
    }
}

用法如下图,将原点放置在左上角区域,25像素中:

// Set the origin for this object at the values specified
maskChoosingSlider.center = maskChoosingSlider.frame.getCenterWhichPlacesFrameOriginAtSpecified_X_and_Y_Coordinates(x_Position: 25, y_Position: 25)

如果您想将 CGPoint 传递给扩展而不是 X 和 Y 坐标,那么您可以自己进行简单的更改。

于 2020-12-16T14:10:46.903 回答