禁用iOS8中单个子视图的自动旋转

Jan*_*Jan 6 objective-c autorotate ios

我正在编写一个绘图应用程序,我不希望绘图视图旋转.同时,我希望其他控件可以很好地旋转,具体取决于设备的方向.在iOS 7中,我通过以下方式解决了这个问题:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    float rotation;

    if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
        rotation = 0;
    }
    else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        rotation = M_PI/2;
    } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
        rotation = -M_PI/2;
    }

    self.drawingView.transform = CGAffineTransformMakeRotation(rotation);
    self.drawingView.frame = self.view.frame;
}
Run Code Online (Sandbox Code Playgroud)

但是在iOS 8上,即使调用该函数并正确设置了转换,它也不会阻止视图旋转.

我已经尝试创建一个视图控制器,它只是阻止它的视图旋转并将它的视图添加到视图层次结构,但它不响应用户输入.

有任何想法吗?

谢谢!

Jan*_*Jan 6

好吧,经过与subviews和transitionCoordinators的一些战斗后,我终于明白了:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    CGAffineTransform targetRotation = [coordinator targetTransform];
    CGAffineTransform inverseRotation = CGAffineTransformInvert(targetRotation);

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        self.drawingView.transform = CGAffineTransformConcat(self.drawingView.transform, inverseRotation);

        self.drawingView.frame = self.view.bounds;
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    }];
}
Run Code Online (Sandbox Code Playgroud)

我所做的是,我计算应用于视图的变换的逆,然后使用它来改变变换.此外,我用视图边界更改框架.这是因为它是全屏的.