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

发布于 2024-09-24 18:10:10 字数 414 浏览 2 评论 0原文

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

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

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

感谢您的帮助和建议!

屏幕截图:

alt text 替代文本

I'm developing an app for iPad and I try to handle multiple orientation.
My app contains a webview and a loading UIImageView that appears when my webview is loading content.

This UIImageView has a background image that I set in InterfaceBuilder. When I change orientation to landscape, the image is cut.

I'd like the UIImageView to set image-portrait.png when the ipad is in portrait mode and image-landscape.png when it's in landscape mode.

Thank you for your help and advices!

Screenshots :

alt text
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

放飞的风筝 2024-10-01 18:10:10

我找到了一个解决方案:

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

I found a solution :

In Interface Builder, I set the autosizing of my ImageView to auto fill the screen.
In my ViewController, I add a method to detect the change of orientation and I set the appropriate image depending if the iPad is in portrait or landscape :

-(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"];
} }
薄荷梦 2024-10-01 18:10:10

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

It took me awhile to understand this concept. I didn't want to create the same image portrait and landscape. The key here is that CGAffineTransformMakeRotation rotates from the original state of your UIImageView or any UIView for that matter. This assumes your background image has orientation to it. E.g. You want your UIImageView to stay put, while other objects behaves to normal orientation change event.

- (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);
    }
}
℉絮湮 2024-10-01 18:10:10

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

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

这将解决您的问题。

You can handle the orientation by autoresizing the view.

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

This will be make solution to your problem.

君勿笑 2024-10-01 18:10:10

虽然 Salah 你的答案对我来说看起来不错,但我相信你可以在这里做两个改进:

  1. 在此函数中设置背景图像:

    <块引用>

    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    

    持续时间(NSTimeInterval)持续时间

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

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

    <块引用>

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

    改进

While Salah your answer looks ok to me i believe you can do two improvements here:

  1. Set the background image within this function:

    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    

    duration (NSTimeInterval)duration

    if you do the change within the didRotateFromInterfaceOrientation
    you will change the background image once you have finished to
    rotate the IPad and the transition from the two background image
    won't be smooth: you will clearly see the new background image popup
    at the end of the rotation.

  2. Improve setting the myImageView.image value:

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

心舞飞扬 2024-10-01 18:10:10

实际上,我会在 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);
      }

I'd actually add another branch to docchang's code as when the iPad is rotated to portrait upside own it uses the portrait right-side-up image which can look a little odd.
I added,

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);
      }
柠北森屋 2024-10-01 18:10:10

有趣的是,程序员很难跳出框框思考(在本例中是字面上的意思)。

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

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

完成。

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

It is interesting that programmers have such a tough time thinking outside the box (literally in this case).

Try this (how I solved this myself).

  1. Create a square image.
  2. Set the constraints of the uiimageview to fill in the screen (leading, trailing, top, bottom)
  3. Set the mode to aspect fill (which will enlarge the image, but keep its aspect ratio constant)

Done.

The key here is, of course, that you should create a square image (which is, as I said above, outside the box ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文