iOS开发教程之UIRefreshControl使用的踩坑指南
2019/7/9 22:41:29
本文主要是介绍iOS开发教程之UIRefreshControl使用的踩坑指南,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
iOS UIRefreshControl基本用法
- (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;
问题描述
接上一个话题,实现了TabBar的点击刷新以后,开始继续写完成功能,刷新UITableView,于是考虑到iOS 10以后,UIScrollView已经有UIRefreshControl的属性了,干脆用自带的写。于是就有了如下的代码:
添加UIRefreshControl到UITableView上去
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; refreshControl.tintColor = [UIColor grayColor]; refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"]; [refreshControl addTarget:self action:@selector(refreshTabView) forControlEvents:UIControlEventValueChanged]; self.newsTableView.refreshControl = refreshControl;
下拉刷新事件
-(void)refreshTabView { //添加一条数据 [self.newsData insertObject:[self.newsData firstObject] atIndex:0]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.newsTableView reloadData]; if ([self.newsTableView.refreshControl isRefreshing]) { [self.newsTableView.refreshControl endRefreshing]; } }); }
TabBar点击事件
-(void)doubleClickTab:(NSNotification *)notification{ //这里有个坑 就是直接用NSInteger接收会有问题 数字不对 //因为上个界面传过来的时候封装成了对象,所以用NSNumber接收后再取值 NSNumber *index = notification.object; if ([index intValue] == 1) { //刷新 [self.newsTableView.refreshControl beginRefreshing]; } }
此时的效果如下,直接下拉刷新可以,但是点击TabBar不可以:
刷新异常情况.gif
分析问题
经过Google帮助,终于知道原因,因为系统自带的UIRefreshControl有两个陷阱:
- 调用-beginRefreshing方法不会触发UIControlEventValueChanged事件;
- 调用-beginRefreshing方法不会自动显示进度圈。
也就是说,只是调用-beginRefreshing方法是不管用的,那么对应的需要做两件事:
- 手动设置UIRefreshControl的事件;
- 手动设置UITableView的ContentOffset,露出进度圈。
解决问题
只需要修改上面第3步中的代码如下:
-(void)doubleClickTab:(NSNotification *)notification{ //这里有个坑 就是直接用NSInteger接收会有问题 数字不对 //因为上个界面传过来的时候封装成了对象,所以用NSNumber接收后再取值 NSNumber *index = notification.object; if ([index intValue] == 1) { //刷新 //animated不要为YES,否则菊花会卡死 [self.newsTableView setContentOffset:CGPointMake(0, self.newsTableView.contentOffset.y - self.newsTableView.refreshControl.frame.size.height) animated:NO]; [self.newsTableView.refreshControl beginRefreshing]; [self.newsTableView.refreshControl sendActionsForControlEvents:UIControlEventValueChanged]; } }
最终效果:
刷新正常情况.gif
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对找一找教程网的支持。
这篇关于iOS开发教程之UIRefreshControl使用的踩坑指南的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-29Axios库教程:初学者必备指南
- 2024-08-29Axios库资料:新手入门指南与基本使用教程
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创
- 2023-11-10【iOS开发】iOS App的加固保护原理:使用ipaguard混淆加固
- 2023-09-30最强大的iOS应用源码保护工具:Ipa Guard,保护你的商业机密代码