iOS UIRefreshControl基本用法
2016-06-22 本文已影响4600人
lingxuemy
- (void) loadRefreshView
{
// 下拉刷新
_refreshControl = [[UIRefreshControl alloc] init];
_refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
[_refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
[self.securityCollectionView addSubview:_refreshControl];
[self.securityCollectionView sendSubviewToBack:_refreshControl];
}
// 设置刷新状态
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
decelerate = YES;
if (scrollView.contentOffset.y < HEIGHT_REFRESH) {
dispatch_async(dispatch_get_main_queue(), ^{
_refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"正在刷新"];
});
[_refreshControl beginRefreshing];
[self loadData];
}
}
// 设置刷新状态
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y >= HEIGHT_REFRESH) {
_refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
}
else if (!scrollView.decelerating) {
_refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"松开刷新"];
}
}
// 结束刷新
- (void) endRefreshControl
{
[_refreshControl endRefreshing];
}
// 刷新的回调方法
- (void) loadData
{
[self endRefreshControl];
// [self performSelector:@selector(endRefreshControl) withObject:nil afterDelay:2];
}
//设置如果collection的内容没有占满整个collectionView,
//这个就不能下拉滑动,没法实现下拉;但是设置下面这个就可以实现下拉了
self.rootView.collectionView.alwaysBounceVertical = YES;