如何将UIScrollview与UIPagecontrol结合使用以显示不同的视图? [英] How to combine UIScrollview with UIPagecontrol to show different views?

查看:110
本文介绍了如何将UIScrollview与UIPagecontrol结合使用以显示不同的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索并搜索了这方面的教程,但没有一个是我正在寻找的。我尝试过Apple的样本,但它只是颜色,我不知道如何使它成为观点。我正在寻找的是一个屏幕,在显示页面控件时会翻页。每次滚动视图页面我希望它显示一个完全不同的按钮视图,很像iPhone的主屏幕。我发现下面的示例代码只适用于图像,但我想修改为使用单独的视图。请帮忙!谢谢。

I've searched and searched for a tutorial for this but none of them are what I'm looking for. I've tried Apple's sample but it is just colors and I don't know how to make it views. All I'm looking for is a screen that will page while showing the page control. Each time the scroll view pages i want it to show a completely different view with buttons, a lot like the home screen of the iPhone. I found the sample code below which works very well with just images, but I'd like to modify to work with separate views. Please Help! Thank you.

- (void)setupPage {
    scrollView.delegate = self;

    [self.scrollView setBackgroundColor:[UIColor clearColor]];
    [scrollView setCanCancelContentTouches:NO];

    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView.clipsToBounds = YES;
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;

    NSUInteger nimages = 0;
    CGFloat cx = 0;
    for (; ; nimages++) {
        NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", (nimages + 1)];                
        UIImage *image = [UIImage imageNamed:imageName];
        if (image == nil) {
            break;
        }
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        CGRect rect = imageView.frame;
        rect.size.height = image.size.height;
        rect.size.width = image.size.width;
        rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
        rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);

        imageView.frame = rect;

        [scrollView addSubview:imageView];
        [imageView release];

        cx += scrollView.frame.size.width;
    }

    self.pageControl.numberOfPages = nimages;
    [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}


推荐答案

我正在尝试这个只是另一天。我仍然习惯使用 UIScrollView 但是这里是如何添加视图到 UIScrollView

I was experimenting with this just the other day. I'm still getting used to using a UIScrollView but here's how you can add views to your UIScrollView:

UIView *blueView = [[UIView alloc] init];
blueView.frame = CGRectMake(100, 0, 500, 1024);
blueView.backgroundColor = [UIColor colorWithRed:164.0/256 green:176.0/256 blue:224.0/256 alpha:1];
[scrollView addSubview:blueView];
[blueView release];

UIView *orangeView = [[UIView alloc] init];
orangeView.frame = CGRectMake(700, 0, 500, 1024);
orangeView.backgroundColor = [UIColor colorWithRed:252.0/256 green:196.0/256 blue:131.1/256 alpha:1];
[scrollView addSubview:orangeView];
[orangeView release];

请注意,我设置 x 每个视图的 frame.origin 中的值,以便它们彼此相邻。您还必须使用 [scrollView setContentSize:CGSizeMake(1200,1024)]; UIScrollView 的内容大小。 c>以便它知道它的子视图有多大。

Notice that I'm setting the x value in frame.origin of each view so that they're sitting adjacent to each other. You also have to set the content size of the UIScrollView with something like [scrollView setContentSize:CGSizeMake(1200, 1024)]; so that it knows how big its subviews are.

然后,如果你需要控制 UIPageControl ,你可以将 numberOfPages 设置为2(对于上面的示例scrollview)并更改其 currentPage 属性。你可以通过实现 scrollViewDidEndDecelerating:来实现这个目的,这是 UIScrollViewDelegate 中的一个方法。您可以通过检查其 contentOffset.x 值来检查scrollview所在的页面。

Then, if you need to control a UIPageControl, you would set its numberOfPages to 2 (for the example scrollview above) and change its currentPage property. You could do this by implementing scrollViewDidEndDecelerating:, which is a method in the UIScrollViewDelegate. You could check which "page" the scrollview is at by checking its contentOffset.x value.

希望这有助于!

这篇关于如何将UIScrollview与UIPagecontrol结合使用以显示不同的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆