본문 바로가기

프로그래밍/iOS

[XCode6][iOS] deviceOrientation에 따른 뷰 설정

사용처는 AVCaptureSession의 카메라 뷰 설정

 

아무리 해도 ios7, 8 둘다 만족하는 코드가 없길래 (사실 XCode5로 하면 기존 소스코드를 사용하면 되지만 XCode6를 사용하는 관계로 ㅈ ㅈ) 오늘 작업함

 

먼저 

- Device Orientation 변화 Notification 받아 오기

 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

 

 - Device Orientation 변화 Notification 제거하기

    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; 

 

- DeviceOrientation 변화를 감지하면 실행하는 함수

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    switch (orientation)

    {

        case UIDeviceOrientationPortrait:

           [self.captureManager.previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];

            self.cameraView.transform = CGAffineTransformIdentity;

            self.cameraView.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);

            self.cameraView.center = [[UIApplication sharedApplication] keyWindow].center;

            self.cameraView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, 0);

            NSLog(@" po %@ %@", NSStringFromCGRect(self.view.frame), NSStringFromCGRect(self.cameraView.frame));

            break;

        case UIDeviceOrientationLandscapeLeft:

            [self.captureManager.previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];

            if (IOS_VERSION_OVER(@"8.0"))

            {

                self.cameraView.transform = CGAffineTransformIdentity;

                self.cameraView.frame = CGRectMake(0, 0, DEVICE_HEIGHT, DEVICE_WIDTH);

                self.cameraView.center = [[UIApplication sharedApplication] keyWindow].center;

                self.cameraView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2);

            }

            else

            {

                self.cameraView.transform = CGAffineTransformIdentity;

                self.cameraView.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);

                self.cameraView.center = [[UIApplication sharedApplication] keyWindow].center;

                self.cameraView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2);

                NSLog(@" left %@ %@", NSStringFromCGRect(self.view.frame), NSStringFromCGRect(self.cameraView.frame));

                self.cameraView.frame = CGRectMake(0, 0, DEVICE_HEIGHT, DEVICE_WIDTH);

            }

            break;

        case UIDeviceOrientationPortraitUpsideDown:

            [self.captureManager.previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];

            self.cameraView.transform = CGAffineTransformIdentity;

            self.cameraView.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);

            self.cameraView.center = [[UIApplication sharedApplication] keyWindow].center;

            self.cameraView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, 0);

            NSLog(@"down %@ %@", NSStringFromCGRect(self.view.frame), NSStringFromCGRect(self.cameraView.frame));

            break;

        case UIDeviceOrientationLandscapeRight:

            [self.captureManager.previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];

            if (IOS_VERSION_OVER(@"8.0"))

            {

                self.cameraView.transform = CGAffineTransformIdentity;

                self.cameraView.frame = CGRectMake(0, 0, DEVICE_HEIGHT, DEVICE_WIDTH);

                self.cameraView.center = [[UIApplication sharedApplication] keyWindow].center;

                self.cameraView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI_2);

            }

            else

            {

                self.cameraView.transform = CGAffineTransformIdentity;

                self.cameraView.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);

                self.cameraView.center = [[UIApplication sharedApplication] keyWindow].center;

                self.cameraView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI_2);

                NSLog(@"right %@ %@", NSStringFromCGRect(self.view.frame), NSStringFromCGRect(self.cameraView.frame));

                self.cameraView.frame = CGRectMake(0, 0, DEVICE_HEIGHT, DEVICE_WIDTH);

            }

            break;

        default:

            break;

    } 

ios8과 ios7이하에서 view를 회전한 뒤 frame이 맞지 않는 경우가 있어 추가 작업한 부분이 있다.