简体   繁体   English

哪种方式更适合移动ImageView

[英]Which way is better to move an ImageView

I would like to move an ImageView around the screen many times, until now I have found two ways. 我想多次在屏幕上移动一个ImageView,直到现在我找到了两种方法。 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;

You could also use CGAffineTransformTranslate, like so: 您也可以使用CGAffineTransformTranslate,如下所示:

imgView.transform = CGAffineTransformIdentity;
imgView.transform = CGAffineTransformTranslate(imgView.transform, 0, -70);
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. 如果您需要调整对象的大小或者想要从左上方给出坐标,我会选择第一个,如果您只是需要移动它并且很舒服给出中心线,那么使用第二种方法似乎更合乎逻辑。

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. 从技术上讲,第二种方式更快一点,因为你不接触图像视图的边界,理论上这可能是昂贵的,这取决于Apple的实现。 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. 但在第二种方式中,视图的帧可能落在像素边界之外(Retina显示规则,不是吗?),这可能导致图像模糊。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM