Rectangle 27 0

ios UICollectionView that only scrolls right (no left, up or down)?


UICollectionViewDelegate
viewDidScroll:
  • Check if your offset puts the first object of your list past the offset (ie. can you still see it on the screen?)
  • If it is, then remove it from the collection view.

This simple implementation might result in choppy animations, you might have to do some optimization once it's working but this should get you started.

You can implement it the same way you would implement an infinite scroll view, by adding / removing items based on the scroll offset.

Note
Rectangle 27 0

ios UICollectionView that only scrolls right (no left, up or down)?


#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    offset = scrollView.contentOffset;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {        

    CGPoint newOffset = scrollView.contentOffset;

    if (newOffset.x < offset.x) {
        // scrolling to the left, reset offset
        [scrollView setContentOffset:offset];
    }
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

    CGPoint newOffset = scrollView.contentOffset;

    if (newOffset.x < offset.x) {
        // scrolling to the left, reset offset with animation
        targetContentOffset->x = offset.x;
    }
}

1/ Since UICollectionViewDelegate conforms to UIScrollViewDelegate, you can get the starting offset of your scrollview with scrollViewWillBeginDragging: then in scrollViewDidScroll: you would compare the new offset's x value. If the new offset.x 's value is smaller than the starting one, set it to 0 and update your scrollview.

2/ Or ou can just use the method scrollViewWillEndDragging:withVelocity:targetContentOffset: which I just spoke about, which sets the offset back to its beginning, with an animation.

3/ You spoke of UISwipeGestureRecognizer, did you give a try to UIPanGestureRecognizer? This is what "simple hold and drag" is.

Because there is inertia with scrolling in iOS, scrollViewDidScroll: is called a lot of time, so it may cause performance issues. You may reduce the number of call by targeting your offset with scrollViewWillEndDragging:withVelocity:targetContentOffset: from UIScrollViewDelegate.

Note