UIScrollView 和 UIView

发布于 2024-10-31 14:55:58 字数 2515 浏览 0 评论 0原文

我想使用 UIScrollView 在用户滚动 UIScrollView 控件时显示多个 UIView。我还不担心显示分页。

我已经设法实现了其中的一些,但没有按照我想要的方式工作。

目前:

我有 3 个具有不同 nib 文件的 ViewController。根视图控制器是带有 UIScrollView 的控制器,它用于加载其余视图控制器。

NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < kNumberOfPages; i++) {
        [controllers addObject:[NSNull null]];
    }
    self.viewControllers = controllers;
    [controllers release];

    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;

    [self loadScrollViewWithPage:0];


- (void)loadScrollViewWithPage:(int)page {
    if (page == 0) {
        PageTwo *controller2 = [viewControllers objectAtIndex:page];
        if ((NSNull *)controller2 == [NSNull null]) {
            controller2 = [[PageTwo alloc] initWithPageNumber:page];
            [viewControllers replaceObjectAtIndex:page withObject:controller2];
            [controller2 release];
        }
        // add the controller's view to the scroll view
        if (nil == controller2.view.superview) {
            CGRect frame = scrollView.frame;
            frame.origin.x = frame.size.width * page;
            frame.origin.y = 0;
            controller2.view.frame = frame;
            [scrollView addSubview:controller2.view];
        }
    }
    if (page == 1) {

    // replace the placeholder if necessary
    NewController *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null]) {
        controller = [[NewController 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;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];
    }
    }

没关系...但是,我想要它做的是首先加载根笔尖。但我不知道该怎么做。我知道我必须将页面数增加到 3,但是当我初始化控制器时,我不知道如何告诉它 page == 0 应该是当前视图。

有什么想法吗?

更新:

叹息,我忽略了一些东西..没有注意到,如果不指定级别 0 的页面,它只会显示当前视图哈哈!

愚蠢的我。

I want to use a UIScrollView to display multiple UIViews as the user scrolls through the UIScrollView control. I'm not worried about showing Pagination just yet.

I already managed to implement some of it, but is not working the way I want it to.

Currently:

I have 3 ViewControllers with different nib files. The root view controller is the one with the UIScrollView, and it to load the rest of the view controllers.

NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < kNumberOfPages; i++) {
        [controllers addObject:[NSNull null]];
    }
    self.viewControllers = controllers;
    [controllers release];

    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;

    [self loadScrollViewWithPage:0];


- (void)loadScrollViewWithPage:(int)page {
    if (page == 0) {
        PageTwo *controller2 = [viewControllers objectAtIndex:page];
        if ((NSNull *)controller2 == [NSNull null]) {
            controller2 = [[PageTwo alloc] initWithPageNumber:page];
            [viewControllers replaceObjectAtIndex:page withObject:controller2];
            [controller2 release];
        }
        // add the controller's view to the scroll view
        if (nil == controller2.view.superview) {
            CGRect frame = scrollView.frame;
            frame.origin.x = frame.size.width * page;
            frame.origin.y = 0;
            controller2.view.frame = frame;
            [scrollView addSubview:controller2.view];
        }
    }
    if (page == 1) {

    // replace the placeholder if necessary
    NewController *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null]) {
        controller = [[NewController 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;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];
    }
    }

That's all fine... However, what I want it to do is to load the root nib first. But I'm not sure how to go about this. I know I have to increase the number of pages to 3, but when I initialize the controllers I don't know how to tell it that page == 0 should be the current view.

Any ideas?

Update:

Sigh, I overlooked something.. Didn't notice that if don't specify a page at level 0 it just shows the current view hah!

Silly me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

跨年 2024-11-07 14:55:58

我忽略了一些东西..没有注意到如果不指定级别 0 的页面它只会显示当前视图哈哈!

I overlooked something.. Didn't notice that if don't specify a page at level 0 it just shows the current view hah!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文