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

6
我正在开发一个iPad应用程序,尝试处理多重方向。我的应用包含一个Webview和一个Loading UIImageView,当我的Webview正在加载内容时,它会出现。
这个UIImageView有一个背景图片,我在InterfaceBuilder中设置。当我改变方向到横向时,图片被裁剪了。
我希望当iPad处于纵向模式时,UIImageView设置image-portrait.png,而当处于横向模式时,设置image-landscape.png。
感谢您的帮助和建议!
截图: alt text alt text

“cut”这个词具体指什么?附上一张图片会更好。 - colithium
我的图像是768x1004的纵向模式。在横向模式下,它仍然是768像素宽,而不是1024像素。我将发布一个屏幕截图。 - Salah
这是竖屏截图:http://img202.imageshack.us/img202/9617/screenshot20100924at141.png,横屏截图:http://img832.imageshack.us/img832/9617/screenshot20100924at141.png。 - Salah
iOS8和自动布局怎么样?我该如何更改背景? - wod
6个回答

7

我找到了一个解决方案:

在界面构建器中,我将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
这是一个使用 UIInterfaceOrientationIsLandscape && UIInterfaceOrientationIsPortrait 的好例子。 - james_womack

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);
    }
}

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];

这将为您的问题提供解决方案。

1

虽然Salah的答案看起来没问题,但我认为你可以在这里做两个改进:

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

    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    

    duration (NSTimeInterval)duration

    如果你在didRotateFromInterfaceOrientation函数中进行更改,那么当你完成iPad旋转并且从两个背景图像之间进行过渡时,你将会更改背景图像一次,而且过渡不会很平滑:你会清楚地看到新的背景图像在旋转结束时弹出。

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

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


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);
      }

0

有趣的是,程序员在思考超越框架(在这种情况下确实如此)时往往会遇到困难。

试试这个方法(这是我自己解决问题的方法)。

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

完成。

关键在于,你应该创建一个正方形图像(就像我上面说的,超越框架)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接