2011-02-04 87 views
1

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

回答

1

從技術上來看,第二種方式是一點點更快,因爲你不觸及圖像視圖的邊界,這在理論上可能是昂貴的,這取決於蘋果公司的實施。但在第二種方式中,視圖的框架可能會落在像素邊界之外(視網膜顯示規則,不是嗎?),這可能會導致模糊的圖像。

但是請注意,結果會有所不同,因爲第一種情況(xNew,yNew)是視圖的左上角,而第二種情況(xNew,yNew)是視圖的中心。

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

imgView.center = CGPointMake(xNew, yNew); 

其實這更是比較的。

如果您需要永遠調整對象的大小或想從左上角給出座標,那麼如果您只需要移動座標並且舒適地給予居中的繩索,那麼我會與第一個一起走,看起來更符合邏輯方法。

2

你也可以使用CGAffineTransformTranslate,就像這樣:

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