开发者

Xcode: UIScrollView with Paging Tweak

I found a great tutorial online that shows you how to page through different pages, well kind of. Here is the link to the tutorial here: http://www.iosdevnotes.com/2011/03/uiscrollview-paging/

The question I have is, how can I tweak his code so that instead of loading "colors" it loads different independent views or view controller files I create? Like I see it loads it's information from the NSArray, but how d开发者_开发知识库o you code it so that you include views or view controllers inside that array, and do you have to add anything else special to make this happen?

Here is the code I'm referring to from his tutorial which is in the implementation file:

- (void)viewDidLoad {
    [super viewDidLoad];

    pageControlBeingUsed = NO;

    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [self.scrollView addSubview:subview];
        [subview release];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);

    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = colors.count;
}


As a quick thought, you could try creating an array or similar of views instead of colours.

Then in the for loop get the views out of the array and use this in the addSubView: message.

This would work for a few views but would not scale well.

I do something similar to this but with UIImageViews to be able to scroll a few images.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜