1
votes

I don't know is that topic had asked before, but I do some search before asking this simple question, something like "UICollectionView cycling cells" "UIColloectionView Infinite Loop".

If the post is duplicated, I am sorry for that...

I had a Horizontal Collection View as a slider in my app, 5 image will be shown, how can I set the function when user scroll to the end (the fifth Items) and slide to right and move to the first cells? also, if the user scroll the first item and slide to left, it will show the fifth item? Thanks.

SDK: iOS 7

Thank alot~

1
Take a look at this question - it will handle the forward scroll, though perhaps not the backward scroll (though it shouldn't be difficult) - pbasdf

1 Answers

0
votes

I had wrote a stupid program to control this

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = _sliderCV.frame.size.width;
    self.pageControl.currentPage = _sliderCV.contentOffset.x / pageWidth;
    NSLog(@"%f", _sliderCV.contentOffset.x);

    CGFloat firstSlide = 0.f;
    CGFloat lastSlide = pageWidth * 4;

    NSLog(@"contentOffset = %f, firstSlide = %f, SelFirstCount = %li",_sliderCV.contentOffset.x, firstSlide, (long)selFirstCount);
    if (_sliderCV.contentOffset.x == firstSlide) {
        if (selFirstCount == 0) {
            selFirstCount += 1 ;
            NSLog(@"%li", (long)selFirstCount);
        } else {
            NSLog(@"Move to Last");
            [_sliderCV setContentOffset:CGPointMake(lastSlide, 0) animated:YES];
            selFirstCount = 0;
            self.pageControl.currentPage = 4;
            selLastCount = 1;
        }
    } else if (_sliderCV.contentOffset.x == lastSlide) {
        if (selLastCount == 0) {
            selLastCount += 1;
        } else {
            [_sliderCV setContentOffset:CGPointMake(firstSlide,0) animated:YES];
            selLastCount = 0;
            self.pageControl.currentPage = 0;
            selFirstCount = 1;
        }
    } else {
        NSLog(@"Clear");
        selFirstCount = 0;
        selLastCount = 0;
    }
}