Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 试图了解自动旋转指针FaceOrientation和UIDeviceOrientationIDChangeNotification应该如何进行_Iphone_Xcode_Device Orientation - Fatal编程技术网

Iphone 试图了解自动旋转指针FaceOrientation和UIDeviceOrientationIDChangeNotification应该如何进行

Iphone 试图了解自动旋转指针FaceOrientation和UIDeviceOrientationIDChangeNotification应该如何进行,iphone,xcode,device-orientation,Iphone,Xcode,Device Orientation,我有一个界面,我想在横向启动。启动后,当用户将设备旋转到纵向时,我将显示一个日视图日历。返回横向时,日历将被取消。在各个方向上,一切都很好,我的应用程序用户界面以横向方向正确显示,日历以纵向方向正确显示 问题在于,如果用户在启动时手持iPhone时是横向的。无论我做什么,我都无法用我的用户界面在横向模式下启动它。My UIDeviceOrientationIDChangeNotification方法触发两次,第一次触发[UIDevice currentDevice]。方向是横向,第二次是纵向。最

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

问题在于,如果用户在启动时手持iPhone时是横向的。无论我做什么,我都无法用我的用户界面在横向模式下启动它。My UIDeviceOrientationIDChangeNotification方法触发两次,第一次触发[UIDevice currentDevice]。方向是横向,第二次是纵向。最终结果是用户界面旋转到纵向模式并显示日视图。不是我想要的。我希望用户界面保持横向,直到用户实际将设备从横向旋转到纵向

我不明白为什么当用户以纵向方向握住设备时,它会以横向[UIDevice currentDevice].方向触发

下面是我的代码在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;  
        }            
    }
}

我不会生成BegingeratingDeviceOrientationNotifications

一个简单的方法是使用BOOL检查肖像何时被允许进入 是否应自动旋转指针面方向

大概是这样的:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {       

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

        return portraitIsAllowed;
    } 

    return YES;
}
然后在其他方法中需要时更改它


请记住,每次用户旋转设备时,以及第一次加载(实例化)控制器时,都会调用shouldAutorotateToInterfaceOrientation。我找到了解决问题的方法。在viewDidLoad中,我启动了一个scheduledTimerWithTimeInterval,并将BegingeratingDeviceOrientationNotifications移动到selector方法

现在,通知不会触发多次。无论设备以何种方式被握住,用户在启动时都会看到风景,启动后所有旋转都能完美工作

这是我修改过的代码。其他一切都没变

- (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];

}

好的,根据你的建议,我把所有东西都移到了ShouldAutorateTointerFaceOrientation。基本上我也有同样的问题。如果用户在启动时将设备保持在纵向模式。当UIInterfaceOrientation设置为横向时,应该自动旋转激发,这会将我的初始化布尔值重置为false,然后激发设置为纵向,这是因为初始化现在为false会触发我的日历例程。我不想要日历,除非用户实际将设备从横向旋转到纵向。如果允许,我会将其标记为已应答。然而,仍然令人好奇的是,为什么shouldAutorotateToInterfaceOrientation和UIDeviceOrientationIDChangeNotification方法在启动时会触发两次,一次在横向,一次在纵向,即使用户拿着设备纵向。他们为什么开枪?设备尚未旋转。我现在猜测,可能是因为我在横向模式下启动状态栏界面,并且设备处于纵向,所以这被检测为旋转。这并不能解释为什么它会发射两次。