2012-03-01 66 views
0

我有一個接口,我想在橫向方向啓動。啓動後,用戶將設備旋轉至縱向時,我正在顯示日視圖日曆。當回到橫向時,日曆被解除。一切工作都很好,我的應用程序用戶界面能夠以橫向顯示正確顯示,並且日曆可以縱向正確顯示。試圖瞭解如何shouldAutorotateToInterfaceOrientation和UIDeviceOrientationDidChangeNotification

問題是如果用戶在啓動時橫向握住iPhone。無論我做什麼,我都無法在橫向模式下使用我的用戶界面啓動它。我的UIDeviceOrientationDidChangeNotification方法觸發兩次,第一次[UIDevice currentDevice] .orientation是橫向,第二次是縱向。最終結果是用戶界面旋轉到肖像模式並顯示日視圖。不是我想要的。我希望用戶界面保持橫向方向,直到用戶將設備從橫向轉爲縱向。

我不明白爲什麼當用戶以縱向方向握住設備時,會出現景觀[UIDevice currentDevice] .orientation。

這裏是我的代碼看起來像在的viewController ...

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  

    if ((interfaceOrientation == UIDeviceOrientationPortrait)|| (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) {    
     return NO; 
    } 

    return YES; 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
     showingCalendar = NO; 
     initializing=YES; 
     [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
     [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(didRotate:) 
              name:UIDeviceOrientationDidChangeNotification 
              object:nil]; 

} 

-(void)didRotate:(NSNotification *)notification { 
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 
    if ((deviceOrientation == UIDeviceOrientationPortrait) || (deviceOrientation == UIDeviceOrientationPortraitUpsideDown)) {    
     if ((!showingCalendar) && (!initializing)) { 
      showingCalendar = YES; 
      [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];    
      GCCalendarPortraitView *calendar = [[[GCCalendarPortraitView alloc] init] autorelease]; 
      navigationController = [[UINavigationController alloc] initWithRootViewController:calendar]; 
      [self presentModalViewController:navigationController animated:YES]; 
     } 
    }else if ((deviceOrientation == UIDeviceOrientationLandscapeRight) || (deviceOrientation == UIDeviceOrientationLandscapeLeft)) { 
     if (showingCalendar) { 
      showingCalendar = NO; 
      if (deviceOrientation == UIDeviceOrientationLandscapeRight){ 
       [self dismissModalViewControllerAnimated:YES]; 
      }else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){ 
       [self dismissModalViewControllerAnimated:YES]; 
      } 
     }else { 
      initializing = NO; 
     }    
    } 
} 

回答

1

我找到了一個解決辦法,以我的問題。在viewDidLoad中,我啓動了scheduledTimerWithTimeInterval並將beginGeneratingDeviceOrientationNotifications移至了選擇器方法。

現在通知永遠不會觸發多次。用戶在啓動時會獲得風景,無論該設備被保持在哪個方式,並且啓動後所有的旋轉都可以很好地工作。

這是我修改後的代碼。其他一切都保持不變...

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    showingCalendar = NO; 
    initializing=YES; 
    timer = [NSTimer scheduledTimerWithTimeInterval:.55 target:self selector:@selector(startOrientationNotifications) userInfo:nil repeats: NO]; 

} 


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

} 
+0

我會在允許時將其標記爲回答。但是,仍然好奇的是,爲什麼shouldAutorotateToInterfaceOrientation和UIDeviceOrientationDidChangeNotification方法在啓動時會觸發兩次,一次是橫向設備方向,一次是縱向,即使用戶握住設備縱向。他們爲什麼開火?該設備尚未旋轉。我現在正在猜測,也許是因爲我在橫向模式下啓動狀態欄的界面,並且設備處於縱向方向,這會作爲旋轉進行檢測。這並不能解釋爲什麼它會發生兩次。 – user278859 2012-03-02 16:33:54

+0

謝謝你。你發現它爲什麼會發射兩次? – portoalet 2012-04-06 05:38:02

0

我不會產生beginGeneratingDeviceOrientationNotifications,

一個簡單的方法可以是使用BOOL檢查時畫像允許 shouldAutorotateToInterfaceOrientation

是這樣的:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  

    if ((interfaceOrientation == UIDeviceOrientationPortrait)|| (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) { 

     return portraitIsAllowed; 
    } 

    return YES; 
} 

然後在其他方法需要時更改它。

而且記住,shouldAutorotateToInterfaceOrientation被稱爲每當用戶旋轉設備,也當加載(實例)控制器首次

+0

好的,考慮到你的建議,我把所有的東西都搬到了shouldAutoRatateToInterfaceOrientation。基本上我有同樣的問題。如果用戶在啓動時將設備保持縱向模式。 shouldAutoRotate觸發UIInterfaceOrientation設置爲橫向,將我的初始化布爾值重置爲false,然後觸發設置爲縱向,因爲現在初始化爲false會啓動我的日曆例程。我不想要日曆,除非用戶實際上將設備從橫向旋轉到縱向。 – user278859 2012-03-01 18:55:20