Ориентация UIDevice постоянно переключается между UIDeviceOrientationPortrait и UIDeviceOrientationLandscapeRight, когда я кладу телефон в горизонтальное положение

Я довольно озадачен значением ориентации устройства, когда я положил телефон в горизонтальном положении. Я зарегистрировал наблюдателя для UIDeviceOrientationDidChangeNotificationи называется beginGeneratingDeviceOrientationNotifications к наблюдателю изменения ориентации устройства. Все работает нормально, пока я не положу свой телефон на рабочий стол.

Вот код в моем проекте:

- (void)deviceDidRotate:(NSNotification *)notification
{
    UIDevice *currentDevice = [UIDevice currentDevice];
    NSLog(@"Orientation: %d", currentDevice.orientation);
}

И журнал дает мне это:

2013-12-29 12:14:28.121 MyProject[1140:60b] Orientation: 5
2013-12-29 12:14:28.764 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:31.609 MyProject[1140:60b] Orientation: 4
2013-12-29 12:14:33.377 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:33.574 MyProject[1140:60b] Orientation: 4
2013-12-29 12:14:33.770 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:33.868 MyProject[1140:60b] Orientation: 4
2013-12-29 12:14:34.065 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:34.164 MyProject[1140:60b] Orientation: 4
2013-12-29 12:14:34.455 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:34.555 MyProject[1140:60b] Orientation: 4
    repeating...

Первые 5 вполне разумны, так как я положил телефон лицевой стороной вверх, но цифры 1 (представляют UIDeviceOrientationPortrait) и 4s (представляют UIDeviceOrientationLandscapeRight) довольно запутанные.

Мой вопрос, как решить эту проблему?

1 ответ

Решение

У меня была та же проблема, следующее решение исправило эту проблему:

Добавьте следующие методы в каждый viewController (с вашей предпочтительной ориентацией)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

- (BOOL)shouldAutorotate {

    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

    return UIInterfaceOrientationLandscapeLeft;
}

И в AppDelegate а также со следующей строкой кода в didFinishLaunchingWithOptions

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredInterfaceOrientationForPresentation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
Другие вопросы по тегам