統計情報(30日間)


最新情報をツイート


人気の投稿

UICollectionViewでエンドレススクロールを実現する方法

このエントリーをはてなブックマークに追加

タネは簡単で(1)先頭と最後にコンテンツの複製を作っておき追加(2)最後へスクロールしたら先頭へ移動・先頭へスクロールしたら最後へ移動する。


スクロール指示のタイミングは -[UIScroll scrollViewDidEndDecelerating:] でハンドリングする。
-(void)scrollViewDidEndDecelerating:(UIScrollview *)scrollview {

    // Calculate where the collection view should be at the right-hand end item
    float contentOffsetWhenFullyScrolledRight = self.collectionView.frame.size.width * ([self.dataArray count] -1);

    if (scrollView.contentOffset.x == contentOffsetWhenFullyScrolledRight) {

        // user is scrolling to the right from the last item to the 'fake' item 1.
        // reposition offset to show the 'real' item 1 at the left-hand end of the collection view

        NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:1 inSection:0];

        [self.collectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

    } else if (scrollView.contentOffset.x == 0)  {

        // user is scrolling to the left from the first item to the fake 'item N'.
        // reposition offset to show the 'real' item N at the right end end of the collection view

        NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:([self.dataArray count] -2) inSection:0];

        [self.collectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

    }
}
なおこの方法が使えるのは paging=YES のケースだけ。



Leave a Reply