哪种方式移动 ImageView 更好
我想在屏幕上多次移动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以使用 CGAffineTransformTranslate,如下所示:
You could also use CGAffineTransformTranslate, like so:
从技术上讲,第二种方法要快一点,因为你不会触及图像视图的边界,理论上这可能会很昂贵,具体取决于苹果的实现。但在第二种方式中,视图的框架可能会超出像素边界(视网膜显示规则,不是吗?),这可能会导致图像模糊。
但请注意,结果会有所不同,因为在第一种情况下 (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.
其实这更多的是比较。
如果您需要调整对象的大小或想要从左上角给出坐标,我会选择第一种方法,如果您只需要移动它并且愿意提供居中的绳索,那么使用第二种方法似乎更合乎逻辑。
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.