试图了解应该如何调整ToootateToInterfaceOrientation和UIDeviceOrientationDidChangeNotification

时间:2012-03-01 09:57:32

标签: iphone xcode device-orientation

我有一个我希望以横向启动的界面。用户将设备旋转为纵向启动后,我正在显示日视图日历。返回横向时,日历将被取消。我的应用程序用户界面在横向方向正确显示,日历在纵向方向正确显示,因此在每个方向都能很好地工作。

问题是用户是否在启动时以横向方向握住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;  
        }            
    }
}

2 个答案:

答案 0 :(得分: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];

}

答案 1 :(得分:0)

我不会生成beginGeneratingDeviceOrientationNotifications,

一种简单的方法可以是使用BOOL来检查何时允许纵向  shouldAutorotateToInterfaceOrientation

类似的东西:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {       

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

        return portraitIsAllowed;
    } 

    return YES;
}

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

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