iOS MJRefresh下拉刷新 上拉加载(可添加gif图版) 小结

1.首先去github上下载最新版 MJ刷新库 下载MJ链接

 

 

2.tableVIew中加入相关代码

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MCAPPWidth, MCAPPHeight - 64)];

_tableView.backgroundColor = myClearColor;

_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

_tableView.delegate = self;

_tableView.dataSource = self;

_tableView.showsVerticalScrollIndicator = NO;

_tableView.scrollsToTop = YES;

[self.view addSubview:_tableView];

[_tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(loadData)];

[_tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

[self gif];

 

//loadData 为你下拉刷新时请求网络数据的方法 loadMoreData为你上拉加载时请求网络数据的方法

 

 

3. [self gif]

 

- (void)gif

{

NSMutableArray *idleImages = [NSMutableArray array];

//这块为你刷新时出现的gif图 需要循环添加到数组中

for (NSUInteger i = 1; i <= 5; i++) {

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@img_loading-%lu.png, (unsigned long)i]];

[idleImages addObject:image];

}

[_tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];

_tableView.gifFooter.refreshingImages = idleImages;

_tableView.gifHeader.updatedTimeHidden = YES;

// 设置即将刷新状态的动画图片(一松开就会刷新的状态)

NSMutableArray *refreshingImages = [NSMutableArray array];

for (NSUInteger i = 1; i <= 5; i++) {

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@img_loading-%lu.png, (unsigned long)i]];

[refreshingImages addObject:image];

}

[_tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];

}

 

 

4. 自定义刷新时顶部底部出现的Label和gif图的frame 这里举footer的例子

MJRefreshGifFooter.m

- (void)layoutSubviews

{

[super layoutSubviews];

// 指示器 gif图的frame

self.gifView.frame = CGRectMake(self.bounds.origin.x + 20, self.bounds.origin.y + 15, self.bounds.size.width / 2, self.bounds.size.height / 2);

 

if (self.stateHidden) {

self.gifView.contentMode = UIViewContentModeCenter;

} else {

self.gifView.contentMode = UIViewContentModeScaleAspectFit;

self.gifView.mj_w = self.mj_w * 0.5 - 90;

}

}

MJRefreshFooter.m

- (void)layoutSubviews

{

[super layoutSubviews];

self.loadMoreButton.frame = self.bounds;

self.loadMoreButton.hidden = YES;

//Label的frame

self.stateLabel.frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y + 15, self.bounds.size.width, self.bounds.size.height / 2);

self.noMoreLabel.frame = self.bounds;

}

 

同理header的gif图frame 和Labelframe 都在相应的类里的方法里 都可以根据需求去修改

 

更多的设置可以看下MJ在github上相关的文档。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Gif-Load-ReTry-Refresh:只需要一张Gif图,一行代码支持初次,重试后再次刷新原理 :遍历View树,在Framelayout中动态插入和移除布局,与生命周期绑定,避免内存泄漏;功能 :目前支持在Activity,Fragment中使用(支持任何方式实现的沉浸式状态栏和透明状态栏);封装 :接口化调用,支持MVP结构中使用(View层implement LoadRetryRefreshListener接口,然后直接在Activity/Fragment传入this即可)。示例Activity中成功Activity中失败在Activity中成功,然后再次刷新在Activity中失败,然后重试成功后刷新Fragment中成功Fragment中失败在Fragment中成功,然后再次刷新在Fragment中失败,然后重试成功后刷新使用   初步配置引入配置属性示例代码在Activity中使用1、注册2、开始3、结果回调4、解除绑定布局代码(勿遗漏第4步,防止内存泄漏)在Fragment中使用1、注册2、开始3、结果回调4、解除绑定布局代码(勿遗漏第4步,防止内存泄漏)反馈与建议初步配置引入Step 1. Add it in your root build.gradle at the end of repositories:allprojects { repositories { ... maven { url 'https://jitpack.io' } } }Step 2. Add the dependencydependencies {         compile 'com.github.NoEndToLF:Gif-Load-ReTry-Refresh:1.1.2' }配置属性方法参数作用setGifR.drawable.*页面的Gif图setBackgroundColorR.color.*页面整体背景颜色setBtnNormalColorR.color.*页面按钮未按下时的颜色setBtnPressedColorR.color.*页面按钮按下时的颜色setBtnBorderColorR.color.*页面按钮边框的颜色setBtnTextColorR.color.*页面按钮文字的颜色setBtnRadiusFloat页面按钮的圆角弧度setBtnTextString页面按钮的显示文字setLoadTextString正在中的提示文字setLoadAndErrorTextColorR.color.*页面的提示文字和失败提示文字的颜色示例代码,建议在 Application的 onCreate中进行初始化LoadRetryRefreshConfig config=new LoadRetryRefreshConfig();         config.setBackgroundColor(R.color.white);         config.setBtnNormalColor(R.color.blue_normal);         config.setBtnPressedColor(R.color.blue_press);         config.setBtnBorderColor(R.color.oringe_normal);         config.setBtnRadius(10f);         config.setBtnText("点击重新");         config.setLoadText("测试2秒钟...");         config.setBtnTextColor(R.color.white);         config.setLoadAndErrorTextColor(R.color.gray);         config.setGif(R.drawable.zhufaner);                 LoadReTryRefreshManager.getInstance().setLoadRetryRefreshConfig(config);在 Activit中使用布局中,请在 Toolbar下的需要的内容最外层套一层 FrameLayout(为何需要这样做),如:<LinearLayout android:layout_width="match_parent"     android:layout_height="match_parent"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:orientation="vertical"     xmlns:android="http://schemas.android.com/apk/res/android">     <android.support.v7.widget.Toolbar         app:contentInsetStart="0dp"         android:layout_width="match_parent"         android:layout_height="?attr/actionBarSize"         android:gravity="center_vertical"         android:id="@ id/toolbar"         android:background="@color/color_toolbar"         >         <FrameLayout             android:layout_width="match_parent"             android:layout_height="match_parent">                          **********************************             **********************************             你的内容布局,如Linearlayout等            **********************************             **********************************                           </FrameLayout></LinearLayout>代码中方法简介方法参数作用registerActivity,LoadRetryRefreshListener注册startLoadActivity开始unRegisterActivity解除绑定onLoadSuccessActivity,ShowRefreshViewListener关闭View和刷新时的Dialog、下刷新等onLoadFailed  Activity,String,ShowRefreshViewListener显示失败原因,关闭View和刷新时的Dialog、下刷新等1、注册,一般在 onCreate中调用LoadReTryRefreshManager.getInstance().register(this, new LoadRetryRefreshListener() {                       @Override             public void loadAndRetry() {                         //执行你的网络请求             //dosomething();             }            @Override             public void showRefreshView() {                         //显示你刷新时的View,如Dialog,下刷新等                              }         });2、开始,无需判断是初次还是刷新,已自动进行判断,只需要在想要刷新的地方直接调用(失败重试已封装到 Button事件中)LoadReTryRefreshManager.getInstance().startLoad(this);3、结果回调,在你的请求成功和失败的回调中结果回调@Override             public void onSuccess(Integer value) {             //成功你要做的事.....                              //结果回调                 LoadReTryRefreshManager.getInstance().onLoadSuccess(FailedActivity.this,                  new ShowRefreshViewListener() {                     @Override                     public void colseRefreshView() {                      //关闭你的刷新View,如Dialog,下刷新等                       }                 });             }             @Override             public void onFailed(Throwable e) {             //失败你要做的事.....                              //结果回调                 LoadReTryRefreshManager.getInstance().onLoadFailed(FailedActivity.this,                  e.getMessage(), new ShowRefreshViewListener() {                     @Override                     public void colseRefreshView() {                        //关闭你的刷新View,如Dialog,下刷新等                     }                 });             }4、解除绑定,可以直接写在 BaseActivity的 onDestory方法中,会自动判断然后进行解绑Override     protected void onDestroy() {                 super.onDestroy();                  LoadReTryRefreshManager.getInstance().unRegister(this);     }在 Fragment中使用布局中,同 Activity中使用一致,请在 Toolbar下的需要的内容最外层套一层 FrameLayout(为何需要这样做)代码中方法简介方法参数作用registerFragment,View,LoadRetryRefreshListener注册(View为Fragment在onCreateView中返回的View)startLoadFragment开始unRegisterFragment解除绑定onLoadSuccessFragment,ShowRefreshViewListener关闭View和刷新时的Dialog、下刷新等onLoadFailedFragment,String,ShowRefreshViewListener显示失败原因,关闭View和刷新时的Dialog、下刷新等1、注册,一般在 onCreateView中调用LoadReTryRefreshManager.getInstance().register(this, contentView,new LoadRetryRefreshListener() {             @Override             public void loadAndRetry() {             //执行你的网络请求             //dosomething();             }             @Override             public void showRefreshView() {             //显示你刷新时的View,如Dialog,下刷新等                              }         });2、开始,无需判断是初次还是刷新,已自动进行判断,只需要在想要刷新的地方直接调用(失败重试已封装到 Button事件中)LoadReTryRefreshManager.getInstance().startLoad(this);3、结果回调,在你的请求成功和失败的回调中结果回调@Override             public void onSuccess(Integer value) {             //成功你要做的事.....                              //结果回调                 LoadReTryRefreshManager.getInstance().onLoadSuccess(FailedActivity.this,                  new ShowRefreshViewListener() {                     @Override                     public void colseRefreshView() {                      //关闭你的刷新View,如Dialog,下刷新等                       }                 });             }             @Override             public void onFailed(Throwable e) {             //失败你要做的事.....                              //结果回调                 LoadReTryRefreshManager.getInstance().onLoadFailed(FailedActivity.this,                  e.getMessage(), new ShowRefreshViewListener() {                     @Override                     public void colseRefreshView() {                        //关闭你的刷新View,如Dialog,下刷新等                     }                 });             }4、解除绑定,可以直接写在 BaseFragment的 onDestroyView方法中,会自动判断然后进行解绑@Override     public void onDestroyView() {         super.onDestroyView();         LoadReTryRefreshManager.getInstance().unRegister(this);     }为何必须在布局中套一层 FrameLayout目前为了在4.4,5.0,6.0,7.0及以上的版本中实现沉浸式状态栏或者是透明式状态栏的适配,实现方式主要在低版本中有所不同,有的是设置全屏然后给Toolbar一个PaddingTop来留出StatusBar的高度,有的是设置全屏StatusBar透明,然后再动态插入一个大小一致的View来占位,达到设置状态栏颜色的目的,因此,如果单纯的在DecorView中来插入布局,难以控制页面的MarginTop,可能会遮盖到Toolbar,所以退而求其次,在布局中需要的部分包一层FrameLayout,再通过递归View树来找到需要添加布局的地方,进行动态插入,这样就不需要处理兼容沉浸式状态栏或者是透明式状态栏的适配造成的问题(当然如果有更好的想法,强烈欢迎Issues或者邮箱建议)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值