【问题标题】:iPad SDK, how to handle orientation with an UIImageViewiPad SDK,如何使用 UIImageView 处理方向
【发布时间】:2011-04-16 17:03:51
【问题描述】:

我正在为 iPad 开发应用程序,并尝试处理多个方向。 我的应用程序包含一个 webview 和一个加载 UIImageView,当我的 webview 加载内容时会出现。

这个 UIImageView 有一个我在 InterfaceBuilder 中设置的背景图像。当我将方向更改为横向时,图像被剪切。

我希望 UIImageView 在 ipad 处于纵向模式时设置 image-portrait.png,在 ipad 处于横向模式时设置 image-landscape.png。

感谢您的帮助和建议!

截图:

【问题讨论】:

标签: iphone ipad sdk uiimageview orientation


【解决方案1】:

我找到了解决办法:

在 Interface Builder 中,我将 ImageView 的自动调整大小设置为自动填充屏幕。 在我的 ViewController 中,我添加了一种检测方向变化的方法,并根据 iPad 是纵向还是横向设置适当的图像:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
    myImageView.image = [UIImage imageNamed:@"image-landscape.png"];
} else  if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
    myImageView.image = [UIImage imageNamed:@"image-portrait.png"];
} }

【讨论】:

  • 这将是一个使用UIInterfaceOrientationIsLandscape && UIInterfaceOrientationIsPortrait的好案例
【解决方案2】:

我花了一段时间才理解这个概念。我不想创建相同的图像纵向和横向。这里的关键是CGAffineTransformMakeRotation 从您的 UIImageView 或任何 UIView 的原始状态旋转。这假设您的背景图像有方向。例如。你希望你的 UIImageView 保持不变,而其他对象的行为是正常的方向更改事件。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                duration:(NSTimeInterval)duration {

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {        
        backgroundImage.transform = CGAffineTransformMakeRotation(M_PI / 2);
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
        backgroundImage.transform = CGAffineTransformMakeRotation(-M_PI / 2);
    }
    else {
        backgroundImage.transform = CGAffineTransformMakeRotation(0.0);
    }
}

【讨论】:

    【解决方案3】:

    您可以通过自动调整视图大小来处理方向。

    UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]];
    
    imageView.frame = self.view.bounds;
    imageView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight
    iimageView.contentMode = UIViewContentModeScaleAspectFill;
    
    [self.view addSubview:imageView];
    [self.view sendSubviewToBack:imageView];
    
    [imageView release];
    

    这将解决您的问题。

    【讨论】:

      【解决方案4】:

      虽然 Salah 你的回答在我看来没问题,但我相信你可以在这里做两个改进:

      1. 在这个函数中设置背景图片:

        -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        

        持续时间(NSTimeInterval)持续时间

        如果您在 didRotateFromInterfaceOrientation 中进行更改 完成后,您将更改背景图像 旋转 iPad 并从两个背景图像过渡 不会很流畅:你会清楚地看到新的背景图片弹出 在轮换结束时。

      2. 改进 myImageView.image 值的设置:

        _myImageView.image = UIInterfaceOrientationIsLandscape(interfaceOrientation) ? [UIImage imageNamed:@"image-landscape.png"] : [UIImage imageNamed:@"image-portrait.png"];

      【讨论】:

        【解决方案5】:

        实际上,我会在 docchang 的代码中添加另一个分支,因为当 iPad 旋转为纵向向上时,它使用纵向右侧向上的图像,这看起来有点奇怪。 我补充说,

        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
          {
          imageBackgroundView.transform = CGAffineTransformMakeRotation(M_PI / 2);
          }
        else
          if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
            {
            imageBackgroundView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
            }
          else
            if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            {
            imageBackgroundView.transform = CGAffineTransformMakeRotation(M_PI);
            }
            else 
              {
              imageBackgroundView.transform = CGAffineTransformMakeRotation(0);
              }
        

        【讨论】:

          【解决方案6】:

          有趣的是,程序员很难跳出框框思考(字面意思是在这种情况下)。

          试试这个(我自己是如何解决的)。

          1. 创建一个方形图像。
          2. 设置 uiimageview 的约束以填充屏幕(前导、尾随、顶部、底部)
          3. 将模式设置为纵横填充(这将放大图像,但保持纵横比不变)

          完成。

          当然,这里的关键是您应该创建一个方形图像(正如我上面所说,在框外;-)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-02-13
            • 1970-01-01
            • 1970-01-01
            • 2011-03-08
            • 1970-01-01
            • 2013-09-09
            • 1970-01-01
            相关资源
            最近更新 更多