2011-06-20 54 views
1

對於以下代碼,我需要根據iphone和ipad方向設置不同的UIIMageView位置。CGRect爲不同的iphone ipad方向

// Animated images - centered on screen 
animatedImages = [[UIImageView alloc] initWithFrame:CGRectMake(
(SCREEN_WIDTH/2) - (IMAGE_WIDTH/2), 
(SCREEN_HEIGHT/2) - (IMAGE_HEIGHT/2) + STATUS_BAR_HEIGHT, 
IMAGE_WIDTH, IMAGE_HEIGHT)]; 

由於CGRect將爲ipad肖像,iphone肖像,ipad風景和iphone風景不同。

如何做到這一點

if (Ipad Portrait orientation) 
{ 
code with different position using CGRectMake (...............) 
} 
if (Iphone Portrait orientation) 
{ 
code with different position using CGRectMake (...............) 
} 
if (Ipad Landscape orientation) 
{ 
code with different position using CGRectMake (...............) 
} 
if (Iphone Landscape orientation) 
{ 
code with different position using CGRectMake (...............) 
} 
+0

請格式化您的文章,並使用代碼標籤,以便閱讀。 – EmilioPelaez

回答

3

你可以使用UIViewController的interfaceOrientation財產。它會給你以下值之一:

UIInterfaceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight

你的代碼是這樣:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) 
{ 
    ... 
} 
else if // etc. 
0

您可以

iPhone和iPad之間的區別
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{ 
    // The device is an iPad running iPhone 3.2 or later. 
} 
else 
{ 
    // The device is an iPhone or iPod touch. 
} 

,你可以用

if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) 
{ 
    // The device is in landscape. 
} 
else 
{ 
    // The device is in portrait. 
} 

縱向和橫向之間區分下面結合成你希望得到的定製。