使用通知监听屏幕的旋转可以有效的避免viewcontrollor判断转向错误的问题

来源:互联网 发布:linux 禁用独显 编辑:程序博客网 时间:2024/06/03 16:54

-(void)viewWillAppear:(BOOL)animated{    UIDevice *device = [UIDevice currentDevice]; //Get the device object    [device beginGeneratingDeviceOrientationNotifications]; //Tell it to start monitoring the accelerometer for orientation    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app    [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:device];}-(void)viewWillDisappear:(BOOL)animated {        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];    UIDevice *device = [UIDevice currentDevice]; //Get the device object    [nc removeObserver:self name:UIDeviceOrientationDidChangeNotification object:device];}- (void)orientationChanged:(NSNotification *)note  {    UIDeviceOrientation  status= [[UIDevice currentDevice] orientation];    switch (status) {        case UIDeviceOrientationPortrait:            // Device oriented vertically, home button on the bottom            [self dismissViewControllerAnimated:YES completion:nil];            break;        case UIDeviceOrientationPortraitUpsideDown:  // Device oriented vertically, home button on the top            break;        case UIDeviceOrientationLandscapeLeft:      // Device oriented horizontally, home button on the right            ;            break;        case UIDeviceOrientationLandscapeRight:      // Device oriented horizontally, home button on the left                        ;            break;        default:            break;    }}



0 0
原创粉丝点击