在iOS8中禁用单个子视图上的自动旋转 [英] Disable autorotate on a single subview in iOS8
问题描述
我正在编写绘图应用程序,我不希望绘图视图旋转。同时,我希望其他控件可以很好地旋转,具体取决于设备的方向。在iOS 7中,我通过以下方式解决了这个问题:
I'm writing a drawing app and I don't want the drawing view to rotate. At the same time, I want other controls to rotate nicely depending on the orientation of the device. In iOS 7 I've solved this via:
- (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;
}
但是在iOS 8上,即使调用了函数并且转换设置正确,它不会阻止视图旋转。
But on iOS 8 even though the function is called and the transform is set correctly, it does not prevent the view from rotating.
我尝试创建一个视图控制器,它只是阻止视图的旋转并将其视图添加到视图层次结构中,但它没有响应用户输入。
I've tried creating a view controller which simply prevents the rotation of it's view and add it's view to the view hierarchy, but then it doesn't respond to user input.
任何想法?
谢谢!
推荐答案
在与subviews和transitionCoordinators进行一些战斗之后,我终于想通了:
Okay after some fighting with subviews and transitionCoordinators I've finally figured it out:
- (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) {
}];
}
我做的是,我计算应用于视图的变换的倒数然后用它来改变变换。此外,我用视图边界更改框架。这是因为它是全屏的。
What I do is, I calculate the inverse of the transform applied to the view and then use it to change the transform. furthermore I change the frame with the view bounds. This is due to it being full screen.
这篇关于在iOS8中禁用单个子视图上的自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!