相机应用程序像屏幕旋转和导航

时间:2015-08-27 15:35:06

标签: ios objective-c cocoa-touch screen-rotation

我想复制原生相机应用程序的确切行为(包括其导航到图库和背面),当设备旋转时,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

有没有办法从viewcontroller指定该视图当前是纵向还是横向?或者有没有办法让屏幕手动旋转而不使用自动调整大小/布局?

2 个答案:

答案 0 :(得分:1)

通过旋转和整形视图来应用旋转。如果将反向旋转和整形应用到视图中,它将显示为不旋转。

通过重塑,我的意思是交换宽度和高度。

旋转到横向时,应用将视图( - )旋转90度的变换并手动交换边界的宽度和高度。您的视图将不再显示为旋转,但界面方向不会受到影响。同时以相反的角度旋转任何子视图,并移动它们以匹配重新塑造的边界。

答案 1 :(得分:0)

看起来Apple从预览打开的库不是导航打开的视图控制器,它是模态打开的视图控制器,它甚至可以从预览打开。只有当您触摸“所有照片”按钮时,才会在此时加载初始照片应用程序,并使用真正的导航栏... (我在iOS 9中检查过)