SVPullToRefresh icon indicating copy to clipboard operation
SVPullToRefresh copied to clipboard

Adding Horizontal Pull to Refresh

Open haashem opened this issue 11 years ago • 6 comments

so it's clear: Is it possible to use it for horizontal ScrollView? like horizontal tableView or CollectionView? should I modify the code? or it is simple as adding for vertical scroll views? I have added to my horizontal collectionView but couldn't see any behavior!

haashem avatar Jun 12 '14 18:06 haashem

Hi! Did you implement this feature? I would like to add the same feature - infinite scroll in horizontal UICollectionView. User scroll collection from right to left and on the last right cell the new portion of data should be fetched from the server. It doesn't work for me.

tevghenii-zz avatar Sep 21 '15 15:09 tevghenii-zz

I have not used SVPullToRefresh. I have implemented mine this way: define a protocol:

- (void)CollectionView:(UICollectionView *)collectionView didChangePullOffset:(CGFloat)offset;
- (void)CollectionViewDidBeginPullingLeft:(UICollectionView *)collectionView withPullOffset:(CGFloat)offset;
- (void)CollectionViewDidBeginPullingRight:(UICollectionView *)collectionView withPullOffset:(CGFloat)offset;
- (void)CollectionViewDidCancelPulling:(UICollectionView *)collectionView;
- (void)CollectionViewDidEndPulling: (UICollectionView *)collectionView;

then implement this in your collectionViewController:

#define PULL_THRESHOLD 160
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat offset = self.collectionView.contentOffset.x;

    [_delegate CollectionView:self.collectionView didChangePullOffset:offset];
    //NSLog(@"bound width: %f",self.collectionView.contentSize.width );
    //NSLog(@"frame width: %f", self.collectionView.frame.size.width);
    if (offset>MAX(0, ABS(self.collectionView.contentSize.width - self.collectionView.frame.size.width + PULL_THRESHOLD)) && !_pulling)
    {
        NSLog(@"%f", self.collectionView.contentSize.width - self.collectionView.frame.size.width + PULL_THRESHOLD);
        //pulling from right to left
        [_delegate CollectionViewDidBeginPullingLeft:self.collectionView withPullOffset:offset];
        NSLog(@"left");
        _pullingDirection = PullingDirectionLeft;
        _pulling = YES;
    }
    else if(offset < -PULL_THRESHOLD && !_pulling)
    {
        //pulling from left to right
        [_delegate CollectionViewDidBeginPullingRight:self.collectionView withPullOffset:offset];
        _pullingDirection = PullingDirectionRight;
        NSLog(@"right");
        _pulling = YES;
    }

    if(_pulling)
    {
        CGFloat pullOffset = MAX(0, offset-PULL_THRESHOLD);
        [_delegate CollectionView:self.collectionView didChangePullOffset:pullOffset];
    }

}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //CGFloat offset = self.collectionView.contentOffset.x;
    if(_pullingDirection == PullingDirectionStable)
    {
        [_delegate CollectionViewDidCancelPulling:self.collectionView];
    }
    else
    {
        [_delegate CollectionViewDidEndPulling:self.collectionView];
    }
    if (!decelerate) {
        [self scrollingEnded];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollingEnded];
}
- (void)scrollingEnded
{
   _pulling = NO;
}

I use these delegate callbacks to adjust my custom pull to refresh animation. or you can use these callbacks to fetch new data from server.

haashem avatar Sep 21 '15 16:09 haashem

Thanks. I created an infinite scroll based on your code.

tevghenii-zz avatar Sep 22 '15 09:09 tevghenii-zz

Can you share your infinite scroll solution for horizontal collection view? I also have same requirement. Thank you.

codeafterhours avatar Oct 07 '15 12:10 codeafterhours

Code hashemp206 wrote in his comment works well. I translated it in Swift and removed some delegate calls I don't need in my project. I recommend to try his code.

tevghenii-zz avatar Oct 07 '15 12:10 tevghenii-zz

Ok I will give it a shot. Thank you :)

codeafterhours avatar Oct 08 '15 04:10 codeafterhours