0
votes

I have a UIView inside a UIViewController the UIView is used to capture photos using AVCaptureSession.

Hence I did not wanted my UIView to rotate when my device rotates, to implement this I wrote this piece of code.

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
 [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
 [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
     CGAffineTransform deltaTransform = coordinator.targetTransform;
     CGFloat deltaAngle = atan2f(deltaTransform.b, deltaTransform.a);
     CGFloat currentRotation = [[self.camera.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
     currentRotation += -1 * deltaAngle + 0.0001;
     [self.camera.layer setValue:@(currentRotation) forKeyPath:@"transform.rotation.z"];
 } 
 completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    CGAffineTransform currentTransform = self.camera.transform;
    currentTransform.a = round(currentTransform.a);
    currentTransform.b = round(currentTransform.b);
    currentTransform.c = round(currentTransform.c);
    currentTransform.d = round(currentTransform.d);
 }];
}

In portrait mode my UIView opens up full screen when I rotate the rotation does not have any effect on my camera UIView and the other UIViews rotate smoothly, But the problem is on rotation to landscape mode my camera UIView is not full screen and takes random size on screen, What am I doing wrong?

Note: I have designed the view using storyboard

1
Did you try disabling the auto-layouts? - atulkhatri
I cannot disable the auto layout I want my other UIView which has the capture button the flash button to rotate properly - Avinash Sharma

1 Answers

0
votes

Auto-Layout layouts the camera view as if it was not rotated on z axis.

I suggest you not to use auto-layout for camera view, and create & add camera view programmatically. Then you can change its frame in animateAlongsideTransition block without dealing with auto-layout constraints.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Create the camera view
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    self.camera = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
    [self.view addSubview:self.camera];
}


-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];



    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        CGAffineTransform deltaTransform = coordinator.targetTransform;
        CGFloat deltaAngle = atan2f(deltaTransform.b, deltaTransform.a);
        CGFloat currentRotation = [[self.camera.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
        currentRotation += -1 * deltaAngle + 0.0001;
        [self.camera.layer setValue:@(currentRotation) forKeyPath:@"transform.rotation.z"];

        // Change the camera view's frame to match the screen
        CGSize screenSize = [[UIScreen mainScreen] bounds].size;
        self.camera.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);

    }
                                 completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
                                     CGAffineTransform currentTransform = self.camera.transform;
                                     currentTransform.a = round(currentTransform.a);
                                     currentTransform.b = round(currentTransform.b);
                                     currentTransform.c = round(currentTransform.c);
                                     currentTransform.d = round(currentTransform.d);


                                 }];


}