Detecting iOS orientation change instantly

后端 未结 6 2174
忘掉有多难
忘掉有多难 2020-12-02 05:38

I have a game in which the orientation of the device affects the state of the game. The user must quickly switch between Landscape, Portrait, and Reverse Landscape orientati

6条回答
  •  我在风中等你
    2020-12-02 06:20

    For my case handling UIDeviceOrientationDidChangeNotification was not good solution as it is called more frequent and UIDeviceOrientation is not always equal to UIInterfaceOrientation because of (FaceDown, FaceUp).

    I handle it using UIApplicationDidChangeStatusBarOrientationNotification:

    //To add the notification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeOrientation:)
    
    //to remove the
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    
     ...
    
    - (void)didChangeOrientation:(NSNotification *)notification
    {
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            NSLog(@"Landscape");
        }
        else {
            NSLog(@"Portrait");
        }
    }
    

提交回复
热议问题