0

我有一个界面,我想以横向启动。启动后,当用户将设备旋转为纵向时,我正在显示一个日视图日历。返回横向时,日历将被关闭。一切都很好,我的应用程序用户界面在横向方向正确显示,日历在纵向方向正确显示。

问题是用户是否在启动时将 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;  
        }            
    }
}
4

2 回答 2

1

我找到了解决我的问题的方法。在 viewDidLoad 我启动了一个 scheduleTimerWithTimeInterval 并将 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];

}
于 2012-03-01T19:20:58.727 回答
0

我不会生成 beginGeneratingDeviceOrientationNotifications,

一种简单的方法是使用 BOOL 检查何时允许在 shouldAutorotateToInterfaceOrientation 中使用肖像

像这样的东西:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {       

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

        return portraitIsAllowed;
    } 

    return YES;
}

然后在需要时在其他方法中更改它。

请记住,每次用户旋转设备以及第一次加载(实例化)控制器时都会调用 shouldAutorotateToInterfaceOrientation

于 2012-03-01T10:29:04.340 回答