关于使用PageControl的ScrollView的几个问题

时间:2012-06-05 08:14:13

标签: ios uinavigationcontroller uiscrollview uitabbarcontroller uipagecontrol

我对iOS开发很陌生,我偶然发现了一些我无法轻易找到答案的问题:

常规设置:我在TabBarApplication

中使用带有PageControl的ScrollView
  1. 是否可以将PageControl与页面内容放在同一区域内?对我来说,它总是被SrollView的视图隐藏,但由于显示空间很少,我真的需要它与实际内容的高度相同。

  2. 我在一些Sandbox-Project中愚弄了,每当我第一次开始在ScrollView-Page的视图中实现一个按钮时,ScrollView的页面就不会立即显示,但只有在第一个之后滚动尝试。我会发布一些代码,但它基本上只能从IB自动生成。

  3. 这是关于可能性的一般性问题:项目的主要设计应该是带有NavigationController的TabBarApplication,让你更深入地了解子菜单,就像它很常见。现在在其中一个选项卡中应该有PageControl,然后您可以通过在NavigationController堆栈上推送Views再次进入子菜单。这可能吗?

  4. 2的一些代码。

    - (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < kNumberOfPages; i++) {
        [controllers addObject:[NSNull null]]; // [TaskPageViewController new]];
    }
    self.viewControllers = controllers;
    [controllers release];
    
    // a page is the width of the scroll view
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;
    
    pageControl.numberOfPages = kNumberOfPages;
    pageControl.currentPage = 0;
    
    }
    - (IBAction)changePage:(id)sender {
    int page = pageControl.currentPage;
    // 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];
    // update the scroll view to the appropriate page
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];
    // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
    pageControlUsed = YES;
    
    }
    
    - (void)loadScrollViewWithPage:(int)page {
    if (page < 0) return;
    if (page >= kNumberOfPages) return;
    
    // replace the placeholder if necessary
    TaskPageViewController *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null]) {
        controller = [[TaskPageViewController alloc] init]; //WithPageNumber: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;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        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 page = floor((scrollView.contentOffset.x - 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
    }
    

1 个答案:

答案 0 :(得分:2)

  1. 您可以为此设置两个视图层次结构:

    • 将页面控件放在scrollview中,原点固定为contentOffset属性
    • 在scrollView的superview中有页面控件,但索引更高(即浮动在它上面)
  2. 这取决于您添加子视图的代码的位置。它是在scrollView的委托方法中吗? viewDidLoad?别的地方?有些代码可能会有所帮助。

  3. 在向下钻取导航时,不确定为什么需要页面控件。页面用于导航相同级别的项目。