iPad SDK,如何使用 UIImageView 处理方向
我正在为 iPad 开发一个应用程序,并尝试处理多个方向。 我的应用程序包含一个 webview 和一个加载 UIImageView,当我的 webview 加载内容时出现。
这个 UIImageView 有一个我在 InterfaceBuilder 中设置的背景图像。当我将方向更改为横向时,图像被剪切。
我希望 UIImageView 在 ipad 处于纵向模式时设置 image-portrait.png,在横向模式时设置 image-landscape.png。
感谢您的帮助和建议!
屏幕截图:
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 :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我找到了一个解决方案:
在 Interface Builder 中,我将 ImageView 的自动调整大小设置为自动填充屏幕。
在我的 ViewController 中,我添加了一个方法来检测方向的变化,并根据 iPad 是纵向还是横向设置适当的图像:
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 :
我花了一段时间才理解这个概念。我不想创建相同的肖像和风景图像。这里的关键是
CGAffineTransformMakeRotation
从 UIImageView 或任何 UIView 的原始状态旋转。这假设您的背景图像具有方向。例如,您希望 UIImageView 保持原状,而其他对象则表现出正常的方向更改事件。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.您可以通过自动调整视图大小来处理方向。
这将解决您的问题。
You can handle the orientation by autoresizing the view.
This will be make solution to your problem.
虽然 Salah 你的答案对我来说看起来不错,但我相信你可以在这里做两个改进:
在此函数中设置背景图像:
<块引用>
持续时间(NSTimeInterval)持续时间
如果您在 didRotateFromInterfaceOrientation 中进行更改
完成后您将更改背景图像
旋转 iPad 并从两个背景图像过渡
不会很顺利:您将清楚地看到新的背景图像弹出窗口
在旋转结束时。
改进 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:
Set the background image within this function:
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.
Improve setting the myImageView.image value:
实际上,我会在 docchang 的代码中添加另一个分支,因为当 iPad 旋转到纵向向上时,它会使用纵向右侧向上的图像,这看起来有点奇怪。
我补充说,
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,
有趣的是,程序员很难跳出框框思考(在本例中是字面上的意思)。
试试这个(我自己是如何解决这个问题的)。
完成。
当然,这里的关键是您应该创建一个方形图像(正如我上面所说,在框外;-)
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).
Done.
The key here is, of course, that you should create a square image (which is, as I said above, outside the box ;-)