iPad SDK,如何使用UIImageView处理方向

时间:2010-09-24 10:19:04

标签: iphone ipad sdk uiimageview orientation

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

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

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

感谢您的帮助和建议!

截图:

alt text alt text

6 个答案:

答案 0 :(得分:7)

我找到了解决方案:

在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"];
} }

答案 1 :(得分:5)

我花了一些时间来理解这个概念。我不想创建相同的图像肖像和风景。这里的关键是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);
    }
}

答案 2 :(得分: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];

这将解决您的问题。

答案 3 :(得分:1)

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

  1. 在此功能中设置背景图像:

    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    
         

    持续时间(NSTimeInterval)持续时间

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

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

      

    _myImageView.image = UIInterfaceOrientationIsLandscape(interfaceOrientation)? [UIImage的   imageNamed:@“image-landscape.png”]:[UIImage   imageNamed:@ “图像portrait.png”];

答案 4 :(得分:0)

我实际上是将另一个分支添加到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);
      }

答案 5 :(得分:0)

有趣的是程序员在开箱即用的时候有这么艰难的时间(在这种情况下确实如此)。

试试这个(我自己如何解决这个问题)。

  1. 创建方形图像。
  2. 设置uiimageview的约束以填充屏幕(前导,尾随,顶部,底部)
  3. 将模式设置为宽高比(将放大图像,但保持其宽高比不变)
  4. 完成。

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