Подтвердить что ты не робот

Как получить текущую ориентацию устройства программно в iOS 6?

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

Вот мой код:

// Autorotation (iOS >= 6.0)
- (BOOL) shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

Именно поэтому я хочу знать, какие методы вызывают при изменении ориентации в iOS.

4b9b3361

Ответ 3

  @property (nonatomic) UIDeviceOrientation m_CurrentOrientation ;

/* Вам необходимо объявить этот код в ViewDidload или ViewWillAppear */

- (void)viewDidLoad

   {

   [super viewDidLoad];

   [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

   [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
}

/* Теперь наше устройство будет предоставлять уведомление, когда мы изменим ориентацию нашего устройства. Таким образом, вы можете контролировать свой код или программу, используя текущую ориентацию */

- (void)deviceOrientationDidChange:(NSNotification *)notification

 {

 //Obtaining the current device orientation

 /* Where self.m_CurrentOrientation is member variable in my class of type UIDeviceOrientation */

 self.m_CurrentOrientation = [[UIDevice currentDevice] orientation];

    // Do your Code using the current Orienation 

 }

Ответ 4

Следуйте документации по UIDevice.

Вам нужно позвонить

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Затем каждый раз, когда ориентация изменяется, вы получаете UIDeviceOrientationDidChangeNotification.

Ответ 5

Этот учебник дает хороший и простой обзор того, как обрабатывать вращение устройства с помощью UIDeviceOrientationDidChangeNotification. Это должно помочь вам понять, как использовать UIDeviceOrientationDidChangeNotification для уведомления, когда ориентация устройства изменилась.

Ответ 6

Может быть, глупо, но работает (только в ViewController):

if (self.view.frame.size.width > self.view.frame.size.height) {
    NSLog(@"Hello Landscape");
}

Ответ 7

В вашем ViewController вы можете использовать метод didRotateFromInterfaceOrientation: для обнаружения, когда устройство было повернуто, а затем делать все, что вам нужно в каждой ориентации.

Пример:

#pragma mark - Rotation

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation) {
        case 1:
        case 2:
            NSLog(@"portrait");
            // your code for portrait...
            break;

        case 3:
        case 4:
            NSLog(@"landscape");
            // your code for landscape...
            break;
        default:
            NSLog(@"other");
            // your code for face down or face up...
            break;
    }
}