哪种方式移动 ImageView 更好

发布于 2024-10-16 01:40:27 字数 610 浏览 6 评论 0原文

我想在屏幕上多次移动 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;

I would like to move an ImageView around the screen many times, until now I have found two ways. However I am not sure which one is more efficient, or maybe both are the same. Please, could I have some advice ? Thanks.

// 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青丝拂面 2024-10-23 01:40:27

您还可以使用 CGAffineTransformTranslate,如下所示:

imgView.transform = CGAffineTransformIdentity;
imgView.transform = CGAffineTransformTranslate(imgView.transform, 0, -70);

You could also use CGAffineTransformTranslate, like so:

imgView.transform = CGAffineTransformIdentity;
imgView.transform = CGAffineTransformTranslate(imgView.transform, 0, -70);
夏夜暖风 2024-10-23 01:40:27

从技术上讲,第二种方法要快一点,因为你不会触及图像视图的边界,理论上这可能会很昂贵,具体取决于苹果的实现。但在第二种方式中,视图的框架可能会超出像素边界(视网膜显示规则,不是吗?),这可能会导致图像模糊。

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

Technically, the second way is a tiny bit faster because you don't touch image views' bounds, which in theory might be expensive, depending on Apple's implementation. But in the second way a view's frame may fall outside pixel boundaries (Retina displays rule, don't they?) which may result in blurry images.

Note, however, that the results will be different because in the first case (xNew, yNew) is an upper left corner of a view, while in the second one (xNew, yNew) is a view's center.

來不及說愛妳 2024-10-23 01:40:27
imgView.frame = CGRectMake(xNew, yNew, image_width, image_height);

imgView.center = CGPointMake(xNew, yNew);

其实这更多的是比较。

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

imgView.frame = CGRectMake(xNew, yNew, image_width, image_height);

imgView.center = CGPointMake(xNew, yNew);

Actually this is more of the comparison.

If you need to ever resize the object or want to give coords from the upper left I would go with the first if you just need to move it and are comfortable giving centered cords it would seem more logical to go with the second method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文