如果我对您的理解正确,您需要设置您有兴趣移动的图像视图的框架。这可以在这样的简单情况下完成:
_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];
任何一种方法都会将图像视图设置到您添加到的父级的左上角。