2009-05-30 62 views
12

我在一個方法中有以下代碼。當我在模擬器中運行它時,調試器跳過代碼?我錯過了什麼?UIDevice方向

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
     ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
{  

} else { 

} 
+0

你的意思是它跳過了,如果和其他人,或者說,它總是轉到別的嗎? – 2009-05-30 17:15:36

+0

是的,有數據在if和else;是的它跳過了整個事情。 – Jordan 2009-05-30 20:14:57

+0

也許它有幫助http://stackoverflow.com/q/634745/194544 – beryllium 2012-02-20 15:35:49

回答

18

更新2

這不應該的問題,而是嘗試開啓方向通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

更新

我的壞,我以爲這是空的。

嘗試刪除或語句,只測試一個方向。看看是否修復它。也許有一個支架問題或一些愚蠢的東西。

我有以下的測試在生產代碼的工作,所以你的技術應該工作:

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
     ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) { 


} 

原來的答案

你必須居然把語句在if塊得到它步入。

調試器非常聰明,可以跳過空白塊。

+0

是否有數據在if和else;是的它跳過了整個事情。 – Jordan 2009-05-30 20:15:00

+0

謝謝。這是我錯過的一個愚蠢的支架問題。真棒。 – Jordan 2009-05-31 13:14:36

23

注意,有一個宏觀UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait,所以不是分開地比較它LandscapeLeft和LandscapeRight你可能只是做這樣的:

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) 
{ 
} 
53

確定接口方向,最好的辦法是看狀態巴方向:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 

    if(orientation == UIInterfaceOrientationPortrait || 
     orientation == UIInterfaceOrientationPortraitUpsideDown) { 

     //Portrait orientation 

} 

if(orientation == UIInterfaceOrientationLandscapeRight || 
    orientation == UIInterfaceOrientationLandscapeLeft) { 

    //Landscape orientation 

} 

類的措施方向基於加速度計,如果設備平放,它不會返回正確的方向。

0

您需要在獲取該值之前先撥打[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] 。看看這個方法的文檔。花了一段時間來跟蹤這個。

2

這樣做沒有打開方向的另一種方式通知將

步驟1:保存當前方向在一個局部變量myCurrentOrientation,併爲其分配是這樣的:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
           duration:(NSTimeInterval)duration 
{ 
    myCurrentOrientation = toInterfaceOrientation; 
} 

步驟2:使用myCurrentOrientation作爲您的支票

if (UIInterfaceOrientationIsLandscape(myCurrentOrientation) == YES) { 
    // landscape 
} 
else { 
    // portrait. 
} 
0

說你是一個跳板的調整中,並希望根據當前應用程序的方向來展示一些東西,那麼你可以使用這個(僅越獄):

UIInterfaceOrientation o = [[UIApplication sharedApplication] _frontMostAppOrientation]; 
0

我建議你使用我的突出顯示的代碼,而不是你的以確保一些代碼行的安全。

-(void) viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self rotations]; 
} 

-(void)rotations 
{ 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(orientationChanged:) 
             name:UIDeviceOrientationDidChangeNotification 
             object:nil]; 
} 

-(void) orientationChanged:(NSNotification *)notification 
{ 
    //USE THIS PART 
    //USE THIS PART 
    //USE THIS PART 
    //USE THIS PART 
    //USE THIS PART 
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) 
    { 
    } 
} 

INSTEAD OF

if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait || 
    [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) 
{ 
} 
0

這裏是要找到方向和屏幕的真正中心的方法。我使用了Tuszy的方法,所以我可以正確設置UIActivityIndi​​catorView。

- (BOOL) isPortraitOrientation { 
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    if(orientation == UIInterfaceOrientationPortrait || 
     orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     return true; 
    } 
    if(orientation == UIInterfaceOrientationLandscapeRight || 
     orientation == UIInterfaceOrientationLandscapeLeft) { 
     return false; 
    } 
    return false; 
} 

而得到中心的方式......

- (void) findMyUIViewCenter { 
    CGPoint myCenter; 
    if ([self isPortraitOrientation]) { 
     myCenter = self.view.center; 
    } 
    else { 
     myCenter = CGPointMake(self.view.frame.size.height/2.0, self.view.frame.size.width/2.0); 
    } 
    NSLog(@"true center -- x:%f y:%f)",myCenter.x,myCenter.y); 
} 
相關問題