gpt4 book ai didi

ios - 关于ScrollView与PageControl的几个问题

转载 作者:行者123 更新时间:2023-11-29 11:12:34 25 4
gpt4 key购买 nike

我是 iOS 开发的新手,我偶然发现了几个我还无法轻易找到答案的问题:

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

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

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

  3. 这又是一个关于可能性的一般性问题:项目的主要设计应该是带有 NavigationController 的 TabBarApplication,让您更深入地了解子菜单,这很常见。现在,在其中一个选项卡中应该有 PageControl,然后您可以通过将 View 推到 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
}

最佳答案

  1. 为此您可以有两个 View 层次结构:

    • 将页面控件置于 scrollview 内,原点固定在 contentOffset 属性
    • 将页面控件放在 scrollView 的父 View 中,但位于更高的索引处(即 float 在其上方)
  2. 这取决于您放置添加 subview 的代码的位置。是在scrollView的delegate方法中吗? viewDidLoad?别的地方?一些代码可能会有所帮助。

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

关于ios - 关于ScrollView与PageControl的几个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10893795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com