【问题标题】:iPhone OS: Rotate just the button images, not the viewsiPhone OS:仅旋转按钮图像,而不是视图
【发布时间】:2012-06-14 04:43:22
【问题描述】:

我正在开发一个 iPad 应用程序,它基本上是一个带有几个按钮的大画布。 (听起来不是很原始,是吗?:P)

无论用户如何握住设备,画布都应保持在原位且不应旋转。实现这一目标的最简单方法是仅支持一个方向。

但是,当设备旋转时,我希望按钮上的图像可以旋转(就像在 iPhone 相机应用程序中一样)。 UIPopoverControllers 还应该使用用户当前的方向(而不是横向显示)。

实现这一目标的最佳方法是什么?

(我想我可以使用 affineTransform 将画布旋转回原位,但我认为这并不理想。)

提前致谢!

【问题讨论】:

    标签: iphone objective-c user-interface


    【解决方案1】:

    只是说出一个想法(不确定它是否可行)......

    也许您可以让屏幕由支持所有方向的UIViewController 控制,但让画布由仅支持单一方向的屏幕控制(即,在其shouldAutorotate... 方法中返回NO)。

    如果这不起作用,我可能会选择 affineTransform 路线。

    【讨论】:

    • 就我的经验而言,嵌套 UIViewControllers 不会使“底部”UIViewController 接收自动旋转方法。我可能也会选择affineTransform
    • @ChriB 很高兴知道!正如我所说,我不确定这是否可行。 :)
    【解决方案2】:

    我发现了一种方法,类似于 Dave DeLong 提出的方法。

    使用转换有效,但并不理想。虽然最终结果(动画结束)是我想要的,但它仍会显示某种摇晃的旋转动画。

    然后我发现了这个:

    https://developer.apple.com/iphone/library/qa/qa2010/qa1688.html

    这表示添加到 WINDOW 的第二个(或第三个等)UIViewController 不会接收旋转事件,因此永远不会旋转。这很有效!

    我创建了一个带有空白视图的“假” UIViewController,并将其添加为第一个视图控制器。这接收旋转事件,然后我将其传递给其他视图控制器,然后这些控制器可以选择是旋转整个视图还是仅旋转按钮标签。

    这有点hacky...但我猜用户不会注意到。

    【讨论】:

      【解决方案3】:

      使用willRotateToInterfaceOrientation 方法,您应该能够使用一些简单的逻辑来交换图像:

      - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      {
          [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
      
          //The following if statement determines if it is an iPad, if it is then the interface orientation is allowed. This line can be taken out to support both iPhones an iPads.
      if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
          if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
              //Swap images and use animations to make the swap look "smooth"
              //NSLog(@"Landscape Right");
      
          } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
              //Swap images and use animations to make the swap look "smooth"
              //NSLog(@"Landscape Left");
      
          } else {
              //Swap images and use animations to make the swap look "smooth"
              //NSLog(@"Portrait");
          }
      } else {
      
      }
      }
      

      以编程方式更改界面中的图像:

      [myUIImageView setImage:[UIImage imageNamed:@"myImage.png"]];
      

      此外,为确保视图不会自动旋转,请使用 shouldAutorotateToInterfaceOrientation 方法告诉您的应用保持纵向。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        • 2017-09-21
        相关资源
        最近更新 更多