Horizontal UICollectionView with UIRefreshControl
Asked Answered
P

3

8

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.

Popover answered 23/6, 2013 at 9:50 Comment(0)
Q
7

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

Quietude answered 27/5, 2015 at 20:55 Comment(3)
This code is outdated. I get the error: 'CGPointMake' is unavailable in Swift It has been explicitly marked unavailable. Any ideas?Noyade
@AngelGarcia CGPointMake has been replaced by CGPoint(x: , y:). In this case would be CGPoint(x: 0, y: 0). Let me know if that helpsQuietude
I suppose the better way would be to place this code into scrollViewDidEndDragging func cuz otherwise the block in if statement will be called multiple times. Also then better approach would be to call your update function inside if statement block with Dispatch after. Something like that: DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { self.presenter.refresh(section: section) }Timberwork
T
2

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");

    }
Tenenbaum answered 30/1, 2017 at 11:38 Comment(0)
O
1

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");

     }
 }
Overcasting answered 2/4, 2014 at 6:50 Comment(1)
Thanks for the code above. Just want to mention it works for when you want to load more data after reaching the end of your scrolling tableview/collectionview, not top.Tenenbaum

© 2022 - 2024 — McMap. All rights reserved.