1

感謝方向改變事先沒有工作

我想檢測時的方向在我的設備改變... 爲我用這個監聽

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

和方向改變的方法是

- (void) orientationChanged:(id)obj 
{ 
    if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     self.view=self.landscapeView; 
     NSLog(@"landscapeView"); 

     //[self loadView1]; 
    } 
    else 
    { 
     self.view = self.portraitView; 
     NSLog(@"portraitView"); 

     //[self loadView1]; 
    }  
} 

每當它進入ViewDidLoad()方法的方向時...

任何一個可以幫助我..

+1

- (空)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation時間:(NSTimeInterval)持續時間會打電話的時候設備方向變化 – Narayana 2012-03-12 07:16:45

回答

1
Try this 
[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
{ 
    //code here  
} 
+0

多數民衆贊成在我正在尋找.. – 2012-03-12 09:23:12

1

變化以下

- (void) orientationChanged:(id)obj 
{ 

    if([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight) 
    { 
    self.view=self.landscapeView; 
    NSLog(@"landscapeView"); 

    //[self loadView1]; 
    } 
    else 
    { 
    self.view = self.portraitView; 
    NSLog(@"portraitView"); 

    //[self loadView1]; 
    }  
} 
0

你爲什麼不只是使用shouldAutorotateToInterfaceOrientation? 這是UIViewController中已調用的方法...

+0

我想打電話給根據不同的看法方向改變... – 2012-03-12 09:23:53

6

您的問題含糊不清,這是一般性解釋。您可以覆蓋下面提到的方法來處理方向下面提到的方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // return YES to supported orientation. 
} 

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
/* Subclasses may override this method to perform additional actions immediately 
    prior to the rotation. For example, you might use this method to disable view 
    interactions, stop media playback, or temporarily turn off expensive drawing or live 
    updates. You might also use it to swap the current view for one that reflects the new 
    interface orientation. When this method is called, the interfaceOrientation property 
    still contains the view’s original orientation. 
*/ 

} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    /*Subclasses may override this method to perform additional actions immediately after 
     the rotation. For example, you might use this method to reenable view interactions, 
     start media playback again, or turn on expensive drawing or live updates. By the time 
     this method is called, the interfaceOrientation property is already set to the new 
     orientation.*/ 
} 


- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 
{ 
    /*This method is called from within the animation block used to rotate the view. 
     You can override this method and use it to configure additional animations that should 
     occur during the view rotation. For example, you could use it to adjust the zoom level 
     of your content, change the scroller position, or modify other animatable properties 
     of your view.*/ 
} 

不要檢查在viewDidLoad中或viewDidAppear設備的方向這樣

if([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft) 
OR if(UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) 

,因爲很多時候它改變

在騎會帶來意想不到的結果,就像iPhone/iPad放在桌子上一樣(我曾經遇到過......非常討厭的問題)。

apple developer link會給更多信息有關處理方向

+0

這個鏈接是非常有用的.. – 2012-03-12 08:03:17

+0

謝謝,我很高興我的回答對你有用。請標記爲正確的答案;) – Krrish 2012-03-12 09:27:50