如何正确处理包含 UIImageViews 的 UIScrollView 的旋转?
我正在开发一个图像查看器,很像照片应用程序。
它是一个 UIScrollView,启用了从互联网加载的图像的分页功能,因此我改编了 LazyTableImages 示例。滚动视图和其中的每个 ImageView 都设置了所有自动调整大小掩码标志。
当我第一次观察旋转期间如何调整大小时,它看起来不错,但是当我开始尝试与滚动视图交互时,我意识到我还必须以编程方式更改 contentView 的大小。我通过在视图控制器中实现 didRotateFromInterfaceOrientation: 来做到这一点。
[self.scrollView setContentSize:CGSizeMake(numberOfImages * portraitWidth, [scrollView bounds].size.height)];
当交互行为正常时,我发现,如果我查看第二张照片并旋转,第一张和第二张照片的部分内容都会显示在屏幕上。我还需要更改 contentOffset。
我尝试通过两种方式解决这个问题 - 既使用 UIScrollView 的scrollRectToVisible:animated: 方法,又尝试直接设置 contentOffset 属性。我已经尝试将此代码放入对方向变化的“一步”和“两步”响应的实现中。例如:
-(void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
[self.scrollView setContentOffset:CGPointMake(currentlyViewedPhotoIndex * largeImageHeight,0) animated:YES];
但在所有情况下,它看起来都非常糟糕。要么我清楚地看到滚动发生,要么它只是跳跃。呜呜呜呜呜呜呜!有没有办法做到这一点,使其行为与照片应用程序完全相同?
I'm developing an image viewer, much like the Photos App.
It's a UIScrollView with paging enabled with images loaded from the internet, so I've adapted portions of the LazyTableImages sample. The Scroll View and each ImageView inside of it have all of their autoresize mask flags set.
When I first observed how resizes were happening during rotation, it looked good, but once I started trying to interact with the scroll view, I realized that I also had to programmatically change the size of the contentView. I did that by implementing didRotateFromInterfaceOrientation: in my view controller.
[self.scrollView setContentSize:CGSizeMake(numberOfImages * portraitWidth, [scrollView bounds].size.height)];
With interaction behaving properly, I then discovered that, if I was viewing the second photo and rotated, portions of both the 1st and 2nd photos would be shown on the screen. I needed to change the contentOffset as well.
I've tried to fix this two ways - both by using the scrollRectToVisible:animated: method of UIScrollView, as well as trying to set the contentOffset property directly. And I've experimented by putting this code in implementations of both the "one-step" and "two-step" responses to changes in Orientation. For example:
-(void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
[self.scrollView setContentOffset:CGPointMake(currentlyViewedPhotoIndex * largeImageHeight,0) animated:YES];
In all cases though, it just looks janky as hell. Either I clearly see the scroll happen, or it just jumps. Uuuuuuuuuuugly! Is there a way to do this so that it behaves exactly like the Photos app does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终做了什么 - 在旋转开始之前,隐藏 UIScrollView 并创建一个包含当前查看的图像的 UIImageView。旋转,该图像将旋转得非常漂亮,当旋转完成时,删除 ImageView 并取消隐藏 Scroll View。
更新 - 如果您今天正在阅读本文(iOS 6 之后的任何时间),请使用 UIPageViewController 并将 transitionStyle 设置为 UIPageViewControllerTransitionStyleScroll,以防万一。
What I wound up doing instead - just before rotation starts, hide the UIScrollView and create a UIImageView that contains the currently viewed image. Rotate, that image will rotate all nice and pretty, and when rotation completes remove the ImageView and unhide the Scroll View.
Update - if you're reading this today (anytime after iOS 6), use a UIPageViewController and set transitionStyle to UIPageViewControllerTransitionStyleScroll, for crissakes.
当面对同样的问题时,我做了一些略有不同的事情。在
willRotateToInterfaceOrientation:duration:
中,我隐藏了除当前显示的子视图之外的所有 UIScrollView 子视图,在didRotateFromInterfaceOrientation:
中,我取消隐藏子视图。I did something slightly different when faced with the same problem. In
willRotateToInterfaceOrientation:duration:
, I hide all of the UIScrollView's subviews except for the currently displayed subview, and indidRotateFromInterfaceOrientation:
I unhide the subviews.