我有一个自定义水平集合视图,有1行,我想添加pull to refresh功能,默认情况下,它出现在我的单元格行上方。我希望用户能够从左到右拉动集合视图以激活UIRefreshControl。有什么想法吗?
提前致谢。
答案 0 :(得分:6)
基本上上面的响应告诉你如何在Objective-C中更多地在UICollectionView中加载。但是,我认为问题是如何在该组件上水平刷新。
我认为你不能水平添加UIRefreshControl,但考虑到之前的代码并转换为Swift我想出了以下代码
请勿忘记将您的UICollectionView弹跳属性设置为
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
})
}
}
另外,为了避免连续拉动的问题,我添加了一些代码来暂时删除弹跳,动画de向右滚动,然后再次启用弹跳。这将为您提供与UIRefreshControl相同的效果。
最后,如果你想要一个加载图标我的建议是将它添加到控制器后面,所以当你拉动你可以看到它背后
答案 1 :(得分:2)
只需添加Julio Bailon的Obj-C版本的答案,该版本适用于将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");
}
答案 2 :(得分:1)
为此,您需要实现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");
}
}