我有一個接口,我想在橫向方向啓動。啓動後,用戶將設備旋轉至縱向時,我正在顯示日視圖日曆。當回到橫向時,日曆被解除。一切工作都很好,我的應用程序用戶界面能夠以橫向顯示正確顯示,並且日曆可以縱向正確顯示。試圖瞭解如何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;
}
}
}
我會在允許時將其標記爲回答。但是,仍然好奇的是,爲什麼shouldAutorotateToInterfaceOrientation和UIDeviceOrientationDidChangeNotification方法在啓動時會觸發兩次,一次是橫向設備方向,一次是縱向,即使用戶握住設備縱向。他們爲什麼開火?該設備尚未旋轉。我現在正在猜測,也許是因爲我在橫向模式下啓動狀態欄的界面,並且設備處於縱向方向,這會作爲旋轉進行檢測。這並不能解釋爲什麼它會發生兩次。 – user278859 2012-03-02 16:33:54
謝謝你。你發現它爲什麼會發射兩次? – portoalet 2012-04-06 05:38:02