Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Horizontal Pull to Refresh #209

Open
haashem opened this issue Jun 12, 2014 · 6 comments
Open

Adding Horizontal Pull to Refresh #209

haashem opened this issue Jun 12, 2014 · 6 comments

Comments

@haashem
Copy link

haashem commented Jun 12, 2014

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!

@tevghenii-zz
Copy link

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.

@haashem
Copy link
Author

haashem commented Sep 21, 2015

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.

@tevghenii-zz
Copy link

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

@codeafterhours
Copy link

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

@tevghenii-zz
Copy link

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.

@codeafterhours
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants