我想复制 native 相机应用程序的确切行为(包括导航到图库和返回),其中当设备旋转时,UI 控件旋转到位而不是整个屏幕旋转。我能够通过以纵向模式锁定屏幕并手动处理设备旋转通知来复制旋转行为,如下所示:
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (void)orientationDidChange:(NSNotification*)note {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
UIInterfaceOrientation newOrientation = UIInterfaceOrientationPortrait;
switch (orientation) {
case UIDeviceOrientationPortraitUpsideDown:
newOrientation = UIInterfaceOrientationPortraitUpsideDown;
break;
case UIDeviceOrientationLandscapeLeft:
newOrientation = UIInterfaceOrientationLandscapeLeft;
break;
case UIDeviceOrientationLandscapeRight:
newOrientation = UIInterfaceOrientationLandscapeRight;
break;
case UIDeviceOrientationPortrait:
newOrientation = UIInterfaceOrientationPortrait;
break;
default:
newOrientation = self.currentOrientation;
break;
}
if (newOrientation == self.currentOrientation) {
return;
}
self.currentOrientation = newOrientation;
[self rotateInterfaceToOrientation:self.currentOrientation];
}
- (void)rotateInterfaceToOrientation:(UIInterfaceOrientation)orientation {
double rotationAngle = 0;
switch (orientation) {
case UIInterfaceOrientationPortraitUpsideDown: rotationAngle = M_PI; break;
case UIInterfaceOrientationLandscapeLeft: rotationAngle = M_PI_2; break;
case UIInterfaceOrientationLandscapeRight: rotationAngle = -M_PI_2; break;
default: rotationAngle = 0; break;
}
CGFloat angle = (float)rotationAngle;
self.defaultTransform = CGAffineTransformMakeRotation(angle);
... manual animation by setting transform
}
这工作得很好并且表现完全符合我的需要。
我的问题与应用程序的屏幕仍然是纵向的事实有关。
整个应用程序支持纵向和横向模式。当我导航到不同的屏幕并返回时,转换被破坏,因为它正在从横向 View 转换到纵向 View 。就在过渡动画开始之前,之前的横向 View 将布局更改为纵向(尽管它被奇怪地拉伸(stretch))。来自模拟器的视频:http://gfycat.com/DeafeningGaseousBrant 。当过渡开始时,您可以看到布局发生变化。它在设备上更加明显,因为您可以一直看到屏幕。可能值得一提的是,我使用自定义转换管理器使屏幕在导航时走向正确的方向(这可能解释了为什么 View 像移动一样移动,但它对有问题的行为没有影响)。
此外,当我用键盘或 UIAlertView 显示提示时,它们的方向是错误的。再次模拟器:http://gfycat.com/SelfreliantPointedEwe .
有什么方法可以从 View Controller 指定 View 当前处于纵向还是横向?或者有没有办法在不使用自动布局调整大小/布局的情况下手动旋转屏幕?
最佳答案
旋转是通过旋转和 reshape View 来应用的。如果您对 View 应用反向旋转和 reshape ,它将显示为不旋转。
通过 reshape ,我的意思是交换宽度和高度。
旋转到横向时,应用将 View 旋转 (-)90 度的变换,并手动交换边界的宽度和高度。您的 View 将不再出现旋转,但界面方向不会受到影响。还可以将任何 subview 旋转相反的角度并移动它们以匹配重新调整形状的边界。
关于ios - 相机应用程序,如屏幕旋转和导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253718/