Is there a documented way to set the iPhone orientation?

后端 未结 17 1367
清歌不尽
清歌不尽 2020-11-22 11:09

I have an app where I would like to support device rotation in certain views but other don\'t particularly make sense in Landscape mode, so as I swapping the views out I wou

17条回答
  •  星月不相逢
    2020-11-22 12:02

    This works for me (thank you Henry Cooke):

    The aim for me was to deal with landscape orientations changes only.

    init method:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    

    - (void)orientationChanged:(NSNotification *)notification {    
        //[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        CGRect bounds = [[ UIScreen mainScreen ] bounds ];
        CGAffineTransform t;
        CGFloat r = 0;
        switch ( orientation ) {
            case UIDeviceOrientationLandscapeRight:
                r = 0;
                NSLog(@"Right");
                break;
            case UIDeviceOrientationLandscapeLeft:
                r  = M_PI;
                NSLog(@"Left");
                break;
            default:return;
        }
    
        t = CGAffineTransformMakeRotation( r );
    
        UIApplication *application = [ UIApplication sharedApplication ];
        [ UIView beginAnimations:@"InterfaceOrientation" context: nil ];
        [ UIView setAnimationDuration: [ application statusBarOrientationAnimationDuration ] ];
        self.view.transform = t;
        self.view.bounds = bounds;
        [ UIView commitAnimations ];
    
        [ application setStatusBarOrientation: orientation animated: YES ];
    }
    

提交回复
热议问题