旋转后正确显示视图

时间:2016-03-20 18:45:16

标签: ios objective-c uiview uiviewcontroller

我在UIViewController中有一个UIView,UIView用于使用AVCaptureSession捕获照片。

因此,当我的设备旋转时,我不希望我的UIView旋转,为了实现这一点,我编写了这段代码。

-(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);
 }];
}

在纵向模式下,我的UIView打开全屏旋转旋转对我的相机UIView没有任何影响,其他UIViews旋转平稳,但问题是旋转到横向模式我的相机UIView不是全屏和在屏幕上采用随机大小,我做错了什么?

注意:我使用storyboard设计了视图

1 个答案:

答案 0 :(得分:0)

自动布局将相机视图布局为好像没有在z轴上旋转。

我建议你不要在相机视图中使用自动布局,并创建&amp;以编程方式添加相机视图。 然后,您可以在animateAlongsideTransition块中更改其框架,而无需处理自动布局约束。

- (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);


                                 }];


}