【问题标题】:problems rotating UIScrollView with paging使用分页旋转 UIScrollView 的问题
【发布时间】:2010-08-29 17:21:21
【问题描述】:

在处理旋转时,我在 iPhone 上使用 UIScrollView 时遇到问题。我使用 Apple 的 PageControl 示例作为模型,但我自己在 View 中创建滚动视图。

1) 在默认纵向加载时,滚动视图按预期工作,从左到右水平滚动

2) 开箱即用,当顺时针旋转并重新初始化时,大部分情况下会将其侧向旋转,现在它垂直向下旋转,而不是所需的水平静止。似乎滚动视图框架的宽度/高度没有改变,只是自身的方向。

3) 我尝试添加一些方向检测逻辑,以根据方向正确调整滚动视图的大小和分页,但前提是我将 CCW 旋转为横向。如果我去 CW,它会从右边开始,然后滚动到左边,事情就不起作用了。

很明显,这越来越不自然了,那么有没有合适的方法来设置我的 UIScrollview 并通过旋转重新初始化它,使其始终从左到右滚动?

- (void)loadView { [self setupPage]; } -(void) setupPage { // lazy init view controllers NSMutableArray *controllers = [[NSMutableArray alloc] init]; for (unsigned i = 0; i = kNumberOfPages) return; // replace the placeholder if necessary MyViewController *controller = [viewControllers objectAtIndex:page]; if ((NSNull *)controller == [NSNull null]) { controller = [[MyViewController alloc] initWithPageNumber:page]; [viewControllers replaceObjectAtIndex:page withObject:controller]; [controller release]; } // add the controller's view to the scroll view if (nil == controller.view.superview) { CGRect frame = scrollView.frame; int x = 0; int y = 0; UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){ x = 0; y = frame.size.height * page; } else { x = frame.size.width * page; y = 0; } frame.origin.x = x; frame.origin.y = y; controller.view.frame = frame; [scrollView addSubview:controller.view]; } } - (void)scrollViewDidScroll:(UIScrollView *)sender { // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in // which a scroll event generated from the user hitting the page control triggers updates from // the delegate method. We use a boolean to disable the delegate logic when the page control is used. if (pageControlUsed) { // do nothing - the scroll was initiated from the page control, not the user dragging return; } // Switch the indicator when more than 50% of the previous/next page is visible CGFloat pageWidth = scrollView.frame.size.width; int offset = 0; UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){ pageWidth = scrollView.frame.size.height; offset = scrollView.contentOffset.y; } else { pageWidth = scrollView.frame.size.width; offset = scrollView.contentOffset.x ; } //CGFloat pageWidth = scrollView.frame.size.width; int page = floor((offset - pageWidth / 2) / pageWidth) + 1; pageControl.currentPage = page; // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling) [self loadScrollViewWithPage:page - 1]; [self loadScrollViewWithPage:page]; [self loadScrollViewWithPage:page + 1]; // A possible optimization would be to unload the views+controllers which are no longer visible } // At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { pageControlUsed = NO; } // At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { pageControlUsed = NO; } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { [self setupPage]; }

【问题讨论】:

    标签: iphone objective-c uikit rotation


    【解决方案1】:

    你没有改变你的滚动视图的 contentSize,所以当它旋转时它不知道它的宽度大于它的高度。

    另外,作为快捷方式,每个 VC 都已经设置了 interfaceOrientation 属性,并且内置了检测方向的函数:UIInterfaceOrientationIsPortrait(self.interfaceOrientation) 和 UIInterfaceOrientationIsLandscape(self.interfaceOrientation)。

    最后,只是视觉上的东西,使用 willRotateToInterfaceOrientation:duration 对眼睛来说更好一些,并且用户永远不会感知到变化,因为它几乎与旋转同时发生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 2012-01-04
      相关资源
      最近更新 更多