检测设备旋转

//MARK: -监听视频旋转通知
-(void)playRotating{
     [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
     [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleDeviceOrientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil
     ];
}
//MARK: -视频旋转action
- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation
{
    //1.获取 当前设备 实例
    UIDevice *device = [UIDevice currentDevice] ;
    switch (device.orientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕朝上平躺");
            break;
        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕朝下平躺");
            break;
            //系統無法判斷目前Device的方向,有可能是斜置
        case UIDeviceOrientationUnknown:
            NSLog(@"未知方向");
            break;
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"屏幕向左横置");
            [self transLeft];
            break;
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"屏幕向右橫置");
            break;
        case UIDeviceOrientationPortrait:
            NSLog(@"屏幕直立");
            [self transVetical];
            break;

        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立,上下顛倒");
            break;

        default:
            NSLog(@"无法辨识");
            break;
    }

}

//释放设备旋转通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIDeviceOrientationDidChangeNotification
                                                  object:nil
     ];
//MARK: - 拍摄视频过程向左旋转
-(void)transLeft{
    [UIView animateWithDuration:0.2f animations:^{
        if (self.flashButton) {
            self.flashButton.transform = CGAffineTransformMakeRotation(M_PI/2);
        }
        if (self.toggleCameraButton) {
             self.toggleCameraButton.transform = CGAffineTransformMakeRotation(M_PI/2);
        }
    }];

}
//MARK: - 拍摄视频过程竖直
-(void)transVetical{
    [UIView animateWithDuration:0.2f animations:^{
        if (self.flashButton) {
            self.flashButton.transform = CGAffineTransformMakeRotation(0);
        }
        if (self.toggleCameraButton) {
            self.toggleCameraButton.transform = CGAffineTransformMakeRotation(0);
        }
    }];

}

你可能感兴趣的:(检测设备旋转)