samvermette / SVPullToRefresh

Give pull-to-refresh & infinite scrolling to any UIScrollView with 1 line of code.
http://samvermette.com/314
MIT License
4.83k stars 1.09k forks source link

Adding Horizontal Pull to Refresh #209

Open haashem opened 10 years ago

haashem commented 10 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

codeafterhours commented 8 years ago

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

tevghenii-zz commented 8 years ago

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 commented 8 years ago

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