【问题标题】:Several Questions about ScrollView with PageControlScrollView with PageControl 的几个问题
【发布时间】:2012-06-05 08:14:13
【问题描述】:

我是 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
}

【问题讨论】:

    标签: ios uinavigationcontroller uiscrollview uitabbarcontroller uipagecontrol


    【解决方案1】:
    1. 您可以为此设置两个视图层次结构:

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

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

    【讨论】:

    • 1.) 谢谢,我会试试的。正如我所说的那样,对整个 Cocoa/Xcode/IB 世界来说都是一个新手。 2.) 它发生在 viewWillAppear 的 UIScrollViewDelegate 类中。该代码基本上来自我从某个论坛下载的示例项目,我只是在页面上添加了按钮,但它突然在应用程序启动时不再加载,我将其中一些作为对上述问题的编辑发布。 3.) 我的目标是一个应用程序来调节你的家庭供暖系统。在主屏幕上会有用于快速改变温度等的按钮。页面用于在加热电路(客厅、卧室)中导航
    • *edit: 2. 它在 loadScrollViewWithPage 自定义方法中(虽然不是我写的)
    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2015-03-15
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多