如何将UIScrollview与UIPagecontrol结合使用以显示不同的视图?

时间:2010-12-09 23:29:27

标签: iphone xcode ios ios4 uipagecontrol

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

- (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)];
}

4 个答案:

答案 0 :(得分:4)

前几天我正在试验这个。我仍然习惯使用UIScrollView,但您可以在此处向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值,以便它们彼此相邻。您还必须使用UIScrollView之类的内容设置[scrollView setContentSize:CGSizeMake(1200, 1024)];的内容大小,以便了解其子视图的大小。

然后,如果您需要控制UIPageControl,则应将其numberOfPages设置为2(对于上面的示例滚动视图)并更改其currentPage属性。您可以通过实施scrollViewDidEndDecelerating:中的UIScrollViewDelegate方法来实现此目的。您可以通过检查其contentOffset.x值来检查滚动视图所在的“页面”。

希望这有帮助!

答案 1 :(得分:0)

这是带有图像的Apple PageControl代码。要使用,只需将图像拖到项目中即可。默认图像是image1.jpg,image2.jpg等。如果您需要png,只需将扩展名更改为.png。

然后用这段代码替换MyViewController.m代码,你就得到了带图像的页面控件。

干杯,乔丹

http://pastebin.com/raw.php?i=c3hS29sC

答案 2 :(得分:0)

//将UIView添加到PageControl示例

  - (id)initWithPageNumber:(int)page {
        UIScreen *screen = [UIScreen mainScreen];
        CGRect frame = [screen applicationFrame];
        frame.origin.y=0;

        if (self = [super initWithNibName:@"MyView" bundle:nil]) {      
                UIView *view = [[UIView alloc] initWithFrame:frame];

                [self.view addSubview:view];

                            // Add whatever you want to the view here.
                [view release];
            pageNumber = page;
        }
        return self;
    }

答案 3 :(得分:0)

这是另一个答案......

NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"Views" owner:self options:nil];
infoView = [nibViews objectAtIndex:0];
[self.view addSubview:infoView];

然后你有了自己的观点。

干杯