Xcode:带有分页调整的 UIScrollView

发布于 2024-12-08 03:30:18 字数 1218 浏览 2 评论 0原文

我在网上找到了一个很棒的教程,它向您展示了如何翻阅不同的页面,很好。以下是教程的链接: http://www.iosdevnotes.com/2011 /03/uiscrollview-paging/

我的问题是,如何调整他的代码,以便加载我创建的不同独立视图或视图控制器文件,而不是加载“颜色”?就像我看到它从 NSArray 加载信息一样,但是如何对其进行编码以便在该数组中包含视图或视图控制器,并且是否需要添加其他特殊内容才能实现这种情况?

这是我在他的教程中引用的代码,该代码位于实现文件中:

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

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 do 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;
}

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

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

发布评论

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

评论(1

如梦亦如幻 2024-12-15 03:30:18

快速思考一下,您可以尝试创建数组或类似的视图而不是颜色。

然后在 for 循环中从数组中获取视图并在 addSubView: 消息中使用它。

这适用于一些视图,但不能很好地扩展。

我做了类似的事情,但使用 UIImageViews 能够滚动一些图像。

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.

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