当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS开发教程之UIRefreshControl使用的踩坑指南

iOS开发教程之UIRefreshControl使用的踩坑指南

2019年07月24日  | 移动技术网IT编程  | 我要评论

8l9958,黄岛招聘会,海王祭好看吗

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网