首页 \ 问答 \ UICollectionView水平滚动(UICollectionView Scrolling Horizontally)

UICollectionView水平滚动(UICollectionView Scrolling Horizontally)

我在我的iOS应用程序中实现了一个UICollectionView。 我有它所以每个单元格是屏幕的宽度,我想拥有它,所以当水平滚动它锁定到可见单元格并将其移动到屏幕的中心。 下面的代码我只适用于第一个单元格。 我不明白如何让这个用于任何用户可见的单元格。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int index  = 0;
    for (TextCell * t in [_tabBarCollectionView visibleCells]) {
        if ((t.center.x>0)&&(t.center.x<[[UIScreen mainScreen]bounds].size.width)) {
            [_tabBarCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
            break;
        }
        index++;
    }

}

I am implementing a UICollectionView in my iOS app. I have it so each cell is the width of the screen and I want to have it so when scrolling horizontally it locks onto the visible cell and moves it to the center of the screen. The code bellow I have only works for the first cell. I don't understand how to get this to work for any cell the user has visible.

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int index  = 0;
    for (TextCell * t in [_tabBarCollectionView visibleCells]) {
        if ((t.center.x>0)&&(t.center.x<[[UIScreen mainScreen]bounds].size.width)) {
            [_tabBarCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
            break;
        }
        index++;
    }

}

原文:https://stackoverflow.com/questions/16256262
更新时间:2023-12-27 10:12

最满意答案

使用Tiles,您正在处理复合模式,而不是像Sitemesh那样使用装饰器模式。

Tiles功能强大且非常灵活,您可以从字符串属性或列表属性到通配符中选择多个选项。 看一下Tiles文档。

另一篇博客文章将向您介绍Tiles的更高级功能,该文章位于tech.finn.no上


With Tiles you are working with the composite pattern, not the decorator pattern as you were with Sitemesh.

Tiles is powerful and very flexible, you have a number options from string attributes, or list attributes to wildcards. Take a look through the Tiles documentation.

Another blog post that takes you through the more advanced capabilities of Tiles is on tech.finn.no

相关问答

更多