【发布时间】:2015-11-22 02:12:50
【问题描述】:
我想复制原生相机应用程序的确切行为(包括其导航到画廊和返回),当设备旋转时,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
}
这很好用,并且完全符合我的需要。
我的问题与应用程序的屏幕仍然是纵向的事实有关。
整个应用程序同时支持纵向和横向模式。当我导航到不同的屏幕并返回时,过渡会中断,因为它正在从横向视图过渡到纵向视图。就在过渡动画开始之前,以前的横向视图将布局更改为纵向(尽管它被奇怪地拉伸了)。来自模拟器的视频:http://gfycat.com/DeafeningGaseousBrant。您可以在过渡开始时看到布局更改。它在设备上更加明显,因为您可以一直看到屏幕。值得一提的是,我正在使用自定义转换管理器来使屏幕在导航时转向正确的方向(这可能解释了为什么视图会像移动一样移动,但它对有问题的行为没有影响)。
当我使用键盘或 UIAlertView 显示提示时,它们的方向是错误的。再次模拟器:http://gfycat.com/SelfreliantPointedEwe。
有没有办法从视图控制器中指定视图当前是纵向还是横向?或者有没有办法在不使用自动布局调整大小/布局的情况下手动旋转屏幕?
【问题讨论】:
标签: ios objective-c cocoa-touch screen-rotation