1

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

常规设置:我在 TabBarApplication 内使用带有 PageControl 的 ScrollView

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

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

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

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
}
4

1 回答 1

2
  1. 为此,您可以有两个视图层次结构:

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

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

于 2012-06-05T08:21:30.457 回答