滚动,缩放UIScrollView和界面方向旋转.如何使用自动调整大小等 [英] Scrolling, zooming UIScrollView and interface orientation rotation. How to use autoresize and more

查看:420
本文介绍了滚动,缩放UIScrollView和界面方向旋转.如何使用自动调整大小等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是很常见的事情,但我无法使其完全正确地工作.

This should be a pretty common thing to do, but I haven't been able to get it to work exactly right.

我有矩形内容.它通常适合320x361:纵向模式减去状态栏减去广告减去标签栏.

I have rectangular content. It normally fits in 320x361: portrait mode minus status bar minus ad minus tab bar.

我已将该内容放在UIScrollView中并启用了缩放.我还希望界面旋转能够正常工作.内容将始终是一个高大的矩形,但是缩放后,用户可能希望一次看到更多的宽度而更少的高度.

I have put that content in a UIScrollView and enabled zooming. I also want interface rotation to work. The content will always be a tall rectangle, but when zoomed users might want to see more width at a time and less height.

要完成此操作,我需要在Interface Builder和代码中做什么?如何在不同的视图上设置自动调整大小?如何设置contentSize和contentInsets?

What do I need to do in Interface Builder and code to get this done? How should I set my autoresizing on the different views? How do I set my contentSize and contentInsets?

我尝试了很多不同的方法,但是没有一种方法是完全正确的.在各种解决方案中,将缩放,界面旋转甚至滚动进行某种组合后,我都遇到了问题,无法再滚动到屏幕上的整个内容.滚动视图将使您回弹,然后才能看到内容的边缘.

I have tried a ton of different ways and nothing works exactly right. In various of my solutions, I've had problems with after some combination of zooming, interface rotation, and maybe scrolling, it's no longer possible to scroll to the entire content on the screen. Before you can see the edge of the content, the scroll view springs you back.

我现在的操作方式大约是80%.也就是说,它应该做的10件事中,有8件事要做.它做错的两件事是:

The way I'm doing it now is about 80% right. That is, out of 10 things it should do, it does 8 of them. The two things it does wrong are:

  1. 在纵向模式下放大时,您可以滚动到内容的边缘,并看到黑色背景.没什么可抱怨的.至少您可以看到所有内容.在横向模式下(无论是否缩放),看到黑色背景超出边缘都是正常现象,因为内容的宽度不足以以1:1缩放级别(最小)填充屏幕.

  1. When zoomed in portrait mode, you can scroll past the edge of the content, and see a black background. That's not too much to complain about. At least you can see all the content. In landscape mode, zoomed or not, seeing the black background past the edge is normal, since the content doesn't have enough width to fill the screen at 1:1 zoom level (the minimum).

当它在运行iOS 3.0的测试设备上运行时,我仍然无法满足内容的要求,但是在运行4.x的我的设备上仍然可以使用. -实际上是以前的解决方案.我的测试人员尚未尝试最新的解决方案.

I am still getting content stuck off the edge when it runs on a test device running iOS 3.0, but it works on mine running 4.x. -- Actually that was with the previous solution. My tester hasn't tried the latest solution.

这是我当前正在使用的解决方案.总而言之,我已经使滚动视图的宽度和高度都达到了任一方向所需的高度,因为我发现手动或自动调整其大小会增加复杂性,而且很脆弱.

Here is the solution I'm currently using. To summarize, I have made the scroll view as wide and tall as it needs to be for either orientation, since I've found resizing it either manually or automatically adds complexity and is fragile.

视图层次结构:

  • 视图
    • scrollView
      • scrollableArea
        • 内容
        • view
          • scrollView
            • scrollableArea
              • content

              视图为320x411,并且启用了所有自动调整大小选项,因此符合屏幕形状

              view is 320x411 and has all the autoresizing options on, so conforms to screen shape

              scrollView是 480 x 361,从原点-80,0开始,仅锁定在顶部并禁用拉伸

              scrollView is 480 x 361, starts at origin -80,0, and locks to top only and disables stretching

              scrollableArea为 480 x 361,并锁定在左侧和顶部.由于scrollView禁用了拉伸,因此其子视图的自动调整大小蒙版无关紧要,但是我还是告诉您.

              scrollableArea is 480 x 361 and locks to left and top. Since scrollView disables stretching, the autoresizing masks for its subviews don't matter, but I tell you anyway.

              内容为320x361,始于原点80,0,并锁定在顶部

              content is 320x361, starts at origin 80,0, and locks to top

              我将scrollView.contentSize设置为480x361.

              I am setting scrollView.contentSize to 480x361.

              shouldAutorotateToInterfaceOrientation支持除纵向颠倒之外的所有方向.

              shouldAutorotateToInterfaceOrientation supports all orientations except portrait upside down.

              在didRotateFromInterfaceOrientation中,如果方向为横向,则将底部内容插入设置为160,否则将其设置为0.如果方向是纵向,我将分别设置80个左右的 indicator 插图,否则将其重置.

              In didRotateFromInterfaceOrientation, I am setting a bottom content inset of 160 if the orientation is landscape, resetting to 0 if not. I am setting left and right indicator insets of 80 each if the orientation is portrait, resetting if not.

              scrollView.minimumZoomScale = 1.0
              
              scrollView.maximumZoomScale = 2.0
              

              viewForZoomingInScrollView返回scrollableArea

              viewForZoomingInScrollView returns scrollableArea

              推荐答案

              // in IB it would be all options activated
              scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
              scrollView.contentSize = content.frame.size; // or bounds, try both
              

              scrollableArea是什么意思?

              您的minZoomScale设置为1.0,这对于纵向模式是很好的,但对于横向模式则不是.因为在风景中您的身高小于人像中的身高,所以您需要将其值小于1.0.对我来说,我使用此实现并每次都调用它,scrollView的框架确实发生了变化:

              your minZoomScale is set to 1.0 thats fine for portrait mode but not for landscape. Because in landscape your height is smaller than in portrait you need to have a value smaller than 1.0. For me I use this implementation and call it every time, the frame of the scrollView did change:

              - (void)setMaxMinZoomScalesForCurrentBounds {
                  CGSize boundsSize = self.bounds.size; // self is a UIScrollView here
                  CGSize contentSize = content.bounds.size;
              
                  CGFloat xScale = boundsSize.width / contentSize.width;
                  CGFloat yScale = boundsSize.height / contentSize.height;
                  CGFloat minScale = MIN(xScale, yScale);
              
                  if (self.zoomScale < minScale) {
                      [self setZoomScale:minScale animated:NO];
                  }
                  if (minScale<self.maximumZoomScale) self.minimumZoomScale = minScale;
                  //[self setZoomScale:minScale animated:YES];
              }
              
              - (void)setFrame:(CGRect)rect { // again, this class is a UIScrollView
                  [super setFrame:rect];
                  [self setMaxMinZoomScalesForCurrentBounds];
              }
              

              这篇关于滚动,缩放UIScrollView和界面方向旋转.如何使用自动调整大小等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆