如何在视图控制器中向水平uicollectionview添加刷新控件?

时间:2015-03-22 00:06:30

标签: ios swift uicollectionview

我目前有:

        refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refresher.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
    self.postsView.addSubview(refresher)

在包含collectionview的viewcontroller的viewdidload()中。与集合视图交互并使自定义单元格显示无瑕疵。但是,当我添加这个复习时,它什么也没做。我不知道为什么,我在这里错过了一步吗?谢谢!

2 个答案:

答案 0 :(得分:2)

尝试以下代码:将其放在UIScrollView扩展中

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
            let offset = scrollView.contentOffset
            let inset = scrollView.contentInset
            let y: CGFloat = offset.x - inset.left
            let reload_distance: CGFloat = -80

            if y < reload

_distance{
            shouldReload = true
        }
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if let _ = scrollView as? UICollectionView {                                
            if shouldReload {
                viewController.showReloading()
                reloadCollectionView()
            }
        }

另一种方法是尝试将UIPanGestureRecoginizer放入(将其添加到collectionView),然后跟踪dy,并检查直到它与-50相距某个X,然后运行一个函数dy> -50,例如。

答案 1 :(得分:0)

实现UIScrollview委托,请尝试以下代码

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

     }
 }