几个 UICollectionViews 像 iBooks 一样水平滚动 [英] Several UICollectionViews Scroll Horizontally like iBooks

查看:21
本文介绍了几个 UICollectionViews 像 iBooks 一样水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含多个 collectionViews 的视图.向左或向右滑动会滚动到另一个 collectionView.

I am trying to do a view that contains several collectionViews. Swiping left or right would scroll to another collectionView.

我已经添加了一个 scrollView 作为启用分页的第一步.我还将内容大小设置为页数.

I have added a scrollView as a first step with paging enabled. I also set the content size to the number of pages.

int nb_of_items = 10;

    // Scroll view
    for (int i=0; i< nb_of_items ; i++) {
        CGRect frame = CGRectMake(_scrollView.frame.size.width * i,
                                  0,
                                  _scrollView.frame.size.width,
                                  _scrollView.frame.size.height);

                _collectionView.frame = frame;
                [_scrollView addSubview:_collectionView];
    }

    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * nb_of_items, _scrollView.frame.size.height);

    _scrollView.delegate = self;

collectionView 在我的故事板中定义,并将数据源和委托设置为 self.我只在第一页上看到了收藏.在其他页面上我什么也得不到.

The collectionView is defined in my storyboard and has the datasource and delegate set to self. I am only getting the collection on the first page. On the other pages I get nothing.

我还尝试在 scrollView 减速时刷新集合

I also tried to refresh the collection when the scrollView decelerates

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updateCollection];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {
        [self updateCollection];
    }
}

-(void)updateCollection{

    NSLog(@"Updating Collection");
    [_collectionView reloadData];

}

有什么想法可以解决这个问题吗?

Any ideas how I could fix that ?

推荐答案

请记住,集合视图是滚动视图,因此您已经具备滚动的能力.您已经知道滚动视图只能将滚动限制为整页.因此,您可以将集合视图设置为使用页面,然后用户可以在集合内容的每个部分之间滚动.

Remember that a collection view is a scroll view, so you already have the ability to scroll. You already know that scroll views can constrain the scrolling to full pages only. So, you can set the collection view to use pages and then the user can scroll between each part of your collection content.

您需要做的就是确保布局以正确的方向滚动(水平而不是垂直)并且每个页面都有适当的内容.您可能想要为此创建自己的布局,但只要您适当地设置项目大小,它就可以与流布局一起使用.

All you need to do is to ensure that the layout scrolls in the correct direction (horizontal instead of vertical) and that each page has the appropriate contents. You probably want to create your own layout for that, though it could work with a flow layout so long as you set the item size appropriately.

这篇关于几个 UICollectionViews 像 iBooks 一样水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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