Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal UICollectionView with UIRefreshControl

I have a custom horizontal collection view that has 1 row and I want to add pull to refresh functionality which, by default, appears above my row of cells. I would like the user to be able to pull the collection view from left to right to activate the UIRefreshControl. Any ideas?

Thanks in advance.

like image 226
CppChase Avatar asked Jun 23 '13 09:06

CppChase


3 Answers

Basically the response above tells you how to do in Objective-C a load more in a UICollectionView. However, I believe the question was how to do pull to refresh horizontally on that component.

I don't think you can add a UIRefreshControl horizontally but taking into consideration the previous code and making a conversion to Swift I came up with the following one

DON'T FORGET TO SET YOUR UICollectionView bounce property TO TRUE

func scrollViewDidScroll(scrollView: UIScrollView) {
    let offset = scrollView.contentOffset
    let inset = scrollView.contentInset
    let y: CGFloat = offset.x - inset.left
    let reload_distance: CGFloat = -75
    if y < reload_distance{
        scrollView.bounces = false
        UIView.animateWithDuration(0.3, animations: { () -> Void in
             scrollView.setContentOffset(CGPointMake(0, 0), animated: false)
        }, completion: { (Bool) -> Void in
             scrollView.bounces = true
        })
    }
}

Also and in order to avoid issues with the continuous pulling I added some code to remove the bouncing temporarily, animate de scroll back to the right and then enabling the bouncing again. That will give you the same effect as the UIRefreshControl.

Finally, if you want to have a loading icon my suggestion is to add it behind the controller so when you pull you can see it behind

like image 60
Julio Bailon Avatar answered Oct 18 '22 07:10

Julio Bailon


Just adding the Obj-C version of Julio Bailon's answer, which works for pulling the collectionView from its Top i.e. Left to Right

    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.bounds;
    CGSize size = scrollView.contentSize;
    UIEdgeInsets inset = scrollView.contentInset;
    float y = offset.x - inset.left;
    float h = size.width;


    float reload_distance = -75; //distance for which you want to load more
    if(y < reload_distance) {
        // write your code getting the more data
        NSLog(@"load more rows");

    }
like image 2
Hyder Avatar answered Oct 18 '22 07:10

Hyder


For this you need to implement the UIScrollViewDelegate method

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     CGPoint offset = scrollView.contentOffset;
     CGRect bounds = scrollView.bounds;
     CGSize size = scrollView.contentSize;
     UIEdgeInsets inset = scrollView.contentInset;
     float y = offset.x + bounds.size.width - inset.right;
     float h = size.width;


     float reload_distance = 75; //distance for which you want to load more
     if(y > h + reload_distance) {
        // write your code getting the more data
        NSLog(@"load more rows");

     }
 }
like image 1
Nitin Avatar answered Oct 18 '22 06:10

Nitin