简体   繁体   English

使用UIRefreshControl的水平UICollectionView

[英]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. 我有一个自定义水平集合视图,有1行,我想添加拉动刷新功能,默认情况下,它出现在我的单元格行上方。 I would like the user to be able to pull the collection view from left to right to activate the UIRefreshControl. 我希望用户能够从左到右拉动集合视图以激活UIRefreshControl。 Any ideas? 有任何想法吗?

Thanks in advance. 提前致谢。

Basically the response above tells you how to do in Objective-C a load more in a UICollectionView. 基本上上面的响应告诉你如何在Objective-C中更多地在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 我不认为你可以水平添加UIRefreshControl,但考虑到以前的代码并转换到Swift我提出了以下一个

DON'T FORGET TO SET YOUR UICollectionView bounce property TO TRUE 不要忘记将您的UICollectionView弹跳属性设置为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. 另外,为了避免连续拉动的问题,我添加了一些代码来暂时删除弹跳,动画de向右滚动,然后再次启用弹跳。 That will give you the same effect as the UIRefreshControl. 这将为您提供与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 最后,如果你想要一个加载图标我的建议是将它添加到控制器后面,所以当你拉动时你可以看到它背后

Just adding the Obj-C version of Julio Bailon's answer, which works for pulling the collectionView from its Top ie Left to Right 只需添加Obio-C版本的Julio Bailon的答案,该版本适用于将collectionView从其顶部拉出,即从左到右

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

    }

For this you need to implement the UIScrollViewDelegate method 为此,您需要实现UIScrollViewDelegate方法

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

     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM