1

我想在屏幕上多次移动 ImageView,直到现在我找到了两种方法。但是我不确定哪个更有效,或者两者可能相同。拜托,我可以给点建议吗?谢谢。

// Create ImageView and add subview
UIImageView imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
imgView.frame = CGRectMake(0, 0, image_width, image_height);
[[self view] addSubview:imgView];   
[imgView release];

// New Coordinates
int xNew = 100;
int yNew = 130;

// First way to move ImageView:
imgView.frame = CGRectMake(xNew, yNew, image_width, image_height);

// Second way to move ImageView:
CGPoint center;
center.x = xNew;
center.y = yNew;
imgView.center = center;
4

3 回答 3

2

你也可以使用 CGAffineTransformTranslate,像这样:

imgView.transform = CGAffineTransformIdentity;
imgView.transform = CGAffineTransformTranslate(imgView.transform, 0, -70);
于 2011-02-04T15:51:41.567 回答
1
imgView.frame = CGRectMake(xNew, yNew, image_width, image_height);

imgView.center = CGPointMake(xNew, yNew);

其实这更多的是比较。

如果您需要调整对象的大小或想要从左上角给出坐标,如果您只需要移动它并且愿意提供居中的绳索,那么我会选择第一种方法,使用第二种方法似乎更合乎逻辑。

于 2011-02-04T15:51:31.113 回答
1

从技术上讲,第二种方法稍微快一点,因为您不会触及图像视图的边界,这在理论上可能会很昂贵,具体取决于 Apple 的实现。但是在第二种方式中,视图的框架可能会落在像素边界之外(Retina 显示规则,不是吗?),这可能会导致图像模糊。

但是请注意,结果会有所不同,因为在第一种情况下 (xNew, yNew) 是视图的左上角,而在第二种情况下 (xNew, yNew) 是视图的中心。

于 2011-02-04T15:54:04.977 回答