【问题标题】:Rotate a UIImageView in an iOS app that does not support rotation在不支持旋转的 iOS 应用中旋转 UIImageView
【发布时间】:2012-10-25 13:51:09
【问题描述】:

我有一个硬编码的应用程序仅支持 1 个方向,纵向。肖像是通过项目设置设置的。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

在这个视图控制器中有几个视图、按钮、标签和其他标准的 iOS 控件。对于此视图控制器中的所有控件,期望的行为是当用户旋转 iOS 设备时,应用程序中的任何内容都不会旋转。

然而,在所有上述视图下方,作为“背景”的是嵌入在 UIScrollView 中的 UIImageView。这是最底部的视图。在任何时候,用户都可以通过启动照片选择器的按钮来更改此背景。 UIImageView 嵌入在 UIScrollView 中,因为所需的行为是允许捏缩放背景图像。

虽然应用程序不支持旋转,但我希望 ImageView 的内容在加载时始终正确定向。

例如 - 请记住,唯一支持的方向是 UIInterfaceOrientationPortrait,用户启动应用程序,将 iOS 设备旋转到 LandScape 位置。在纵向用户选择新的背景图像时。预期的行为将是根据当前方向选择要定向和纵横比正确的图像。

本质上,我正在寻找该背景图像在被选中时始终具有朝向地球的“重力”。如果用户选择新图像后旋转 iOS 设备,则背景图像不会旋转。只有在最初选择图像时,它才会正确定位。

我可以使用 CGAffineTransformMakeRotation 旋转图像,但是通过捏合放大会出现不良行为,因为图像本身的像素并没有真正改变方向。

我想我可能需要在用户选择图像时实际创建一个新图像,类似于...

UIImage * RightLandscapeImage = [[UIImage alloc] initWithCGImage: userSelectedImage.CGImage
                                                         scale: 1.0
                                                   orientation: UIImageOrientationRight];

我没有看到任何简单的解决方案来解决这个问题。可能我忽略了一些简单的事情?

FOLLOW UP 这似乎是必需的代码。考虑到当前方向和启动方向。

UIImage * newImage;
if( startUpOrientation == UIInterfaceOrientationLandscapeRight )
{
    switch (currentOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
          newImage = [[UIImage alloc] initWithCGImage: bgImage.CGImage
                                                                 scale: 1.0
                                                           orientation: UIImageOrientationDownMirrored];

             [self.backgroundImageView setImage:newImage];
            break;
        case UIInterfaceOrientationLandscapeRight:
             [self.backgroundImageView setImage:bgImage];
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            newImage = [[UIImage alloc] initWithCGImage: bgImage.CGImage
                                                  scale: 1.0
                                            orientation: UIImageOrientationRight];

            [self.backgroundImageView setImage:newImage];

            break;
        default:
            newImage = [[UIImage alloc] initWithCGImage: bgImage.CGImage
                                                  scale: 1.0
                                            orientation: UIImageOrientationLeft];
            [self.backgroundImageView setImage:newImage];
            break;
    }
 }

【问题讨论】:

    标签: ios uiscrollview uiimageview uiinterfaceorientation uiimageorientation


    【解决方案1】:

    您可以在 willRotateToInterfaceOrientation 中将变换应用于要旋转的任何视图

    你可以试试这个代码:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
        {
            switch (toInterfaceOrientation) {
                case UIInterfaceOrientationLandscapeLeft:
                    yourview.transform = CGAffineTransformMakeRotation(M_PI_2); // 90 degress
                    break;
                case UIInterfaceOrientationLandscapeRight:
                    yourview.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2); // 270 degrees
                    break;
                case UIInterfaceOrientationPortraitUpsideDown:
                    yourview.transform = CGAffineTransformMakeRotation(M_PI); // 180 degrees
                    break;
                default:
                    yourview.transform = CGAffineTransformMakeRotation(0.0);
                    break;
            }
        }
    

    【讨论】:

    • 谢谢。这有点帮助。然而,奇怪的是,我第一次这样做时就失去了捏缩放的能力。再做一次将重新启用捏缩放,但不是第一次?
    • 我不确定你的意思,你至少在旋转设备一次后得到适当的缩放,从那时起可以吗?如果是,则在显示视图后立即为当前方向设置变换,例如在 viewWillAppear 调用 [self willRotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0] 中。看看有没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2014-01-14
    • 2012-09-08
    相关资源
    最近更新 更多