【问题标题】:Show the view properly after rotation旋转后正确显示视图
【发布时间】:2016-03-20 18:45:16
【问题描述】:

我在 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 和其他 UIView 旋转没有任何影响,但问题是在旋转到横向模式时,我的相机 UIView 不是全屏并且在屏幕上随机显示大小,我做错了什么?

注意:我使用情节提要设计了视图

【问题讨论】:

  • 您是否尝试禁用自动布局?
  • 我无法禁用自动布局我希望我的其他 UIView 具有捕获按钮和闪光灯按钮正确旋转

标签: ios objective-c uiview uiviewcontroller


【解决方案1】:

自动布局布局相机视图,就好像它没有在 z 轴上旋转一样。

我建议您不要对相机视图使用自动布局,而是以编程方式创建和添加相机视图。 然后您可以在 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);


                                 }];


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多