![(logo)](https://camo.githubusercontent.com/96b5eb9da2f89ce420e3d8428947944cbb9adefe8f8f5ae35ef895b607a1b19d/687474703a2f2f696d616765732e636e6974626c6f672e636f6d2f626c6f67323031352f3439373237392f3230313530352f3035313030343439323034333338352e706e67)
MJRefresh
- An easy way to use pull-to-refresh
- 用法简单的下拉刷新框架:一行代码搞定
Contents
- Getting Started
- 常见API
- Examples
- 期待
支持哪些控件的刷新
UIScrollView
、UITableView
、UICollectionView
、UIWebView
如何使用MJRefresh
- cocoapods导入:
pod 'MJRefresh'
- 手动导入:
- 将
MJRefresh
文件夹中的所有文件拽入项目中
- 导入主头文件:
#import "MJRefresh.h"
Base Custom
MJRefresh.bundle MJRefresh.h
MJRefreshConst.h MJRefreshConst.m
UIScrollView+MJExtension.h UIScrollView+MJExtension.m
UIScrollView+MJRefresh.h UIScrollView+MJRefresh.m
UIView+MJExtension.h UIView+MJExtension.m
已经超过上百个App正在使用MJRefresh
![](https://camo.githubusercontent.com/870a7c91d3da99fb4493ff4bfff8763ebfc01ef7d4e678ed0445191889eadb72/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313231323336353034313635302e706e67)
* 更多App信息可以关注:[M了个J-博客园](http://www.cnblogs.com/mjios/p/4409853.html)
MJRefresh类结构图
![](https://camo.githubusercontent.com/c839b424e7dfa6e86f74fc61f9d66ad3f37d2ba209a1896b9afb5ecb24ae7f36/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3133323233323435363133393137372e706e67)
- 图中
红色文字的类
:可以直接拿来用
- 下拉刷新控件的种类
- 默认(Normal):
MJRefreshNormalHeader
- 动图(Gif):
MJRefreshGifHeader
- 上拉刷新控件的种类
- 自动刷新(Auto)
- 默认(Normal):
MJRefreshAutoNormalFooter
- 动图(Gif):
MJRefreshAutoGifFooter
- 自动回弹(Back)
- 默认(Normal):
MJRefreshBackNormalFooter
- 动图(Gif):
MJRefreshBackGifFooter
- 图中
非红色文字的类
:拿来继承,用于自定义刷新控件
- 关于如何自定义刷新控件,可以参考下图的类
![](https://camo.githubusercontent.com/8d4b0109f4a651674c90e6c195d7c54418ab1a538bf601eb38d25b763cf96ddf/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313335383135393130373839332e706e67)
MJRefreshComponent.h
/** 刷新控件的基类 */
@interface MJRefreshComponent : UIView
#pragma mark - 刷新状态控制
/** 进入刷新状态 */
- (void)beginRefreshing;
/** 结束刷新状态 */
- (void)endRefreshing;
/** 是否正在刷新 */
- (BOOL)isRefreshing;
#pragma mark - 其他
/** 根据拖拽比例自动切换透明度 */
@property (assign, nonatomic, getter=isAutomaticallyChangeAlpha) BOOL automaticallyChangeAlpha;
@end
MJRefreshHeader.h
@interface MJRefreshHeader : MJRefreshComponent
/** 创建header */
+ (instancetype)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock;
/** 创建header */
+ (instancetype)headerWithRefreshingTarget:(id)target refreshingAction:(SEL)action;
/** 这个key用来存储上一次下拉刷新成功的时间 */
@property (copy, nonatomic) NSString *lastUpdatedTimeKey;
/** 上一次下拉刷新成功的时间 */
@property (strong, nonatomic, readonly) NSDate *lastUpdatedTime;
/** 忽略多少scrollView的contentInset的top */
@property (assign, nonatomic) CGFloat ignoredScrollViewContentInsetTop;
@end
MJRefreshFooter.h
@interface MJRefreshFooter : MJRefreshComponent
/** 创建footer */
+ (instancetype)footerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock;
/** 创建footer */
+ (instancetype)footerWithRefreshingTarget:(id)target refreshingAction:(SEL)action;
/** 提示没有更多的数据 */
- (void)endRefreshingWithNoMoreData;
/** 重置没有更多的数据(消除没有更多数据的状态) */
- (void)resetNoMoreData;
/** 忽略多少scrollView的contentInset的bottom */
@property (assign, nonatomic) CGFloat ignoredScrollViewContentInsetBottom;
/** 自动根据有无数据来显示和隐藏(有数据就显示,没有数据隐藏) */
@property (assign, nonatomic) BOOL automaticallyHidden;
@end
MJRefreshAutoFooter.h
@interface MJRefreshAutoFooter : MJRefreshFooter
/** 是否自动刷新(默认为YES) */
@property (assign, nonatomic, getter=isAutomaticallyRefresh) BOOL automaticallyRefresh;
/** 当底部控件出现多少时就自动刷新(默认为1.0,也就是底部控件完全出现时,才会自动刷新) */
@property (assign, nonatomic) CGFloat triggerAutomaticallyRefreshPercent;
@end
参考
* 由于这个框架的功能较多,就不写具体文字描述其用法
* 大家可以直接参考示例中的MJTableViewController、MJCollectionViewController、MJWebViewController,更为直观快速
![](https://camo.githubusercontent.com/a9b3aa4980abe7beddcce075e16363ae3883b80b8d26417cbbcb281b2262d9f9/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313334353437303034383132302e706e67)
下拉刷新01-默认
self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
// 进入刷新状态后会自动调用这个block
}];
或
// 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
// 马上进入刷新状态
[self.tableView.header beginRefreshing];
![(下拉刷新01-普通)](https://camo.githubusercontent.com/80f8640cd93b315757f0292070f4c708f3b2ee2a625572046ffa6a584cb52b5a/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230343334333438363135312e676966)
下拉刷新02-动画图片
// 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
// 设置普通状态的动画图片
[header setImages:idleImages forState:MJRefreshStateIdle];
// 设置即将刷新状态的动画图片(一松开就会刷新的状态)
[header setImages:pullingImages forState:MJRefreshStatePulling];
// 设置正在刷新状态的动画图片
[header setImages:refreshingImages forState:MJRefreshStateRefreshing];
// 设置header
self.tableView.mj_header = header;
![(下拉刷新02-动画图片)](https://camo.githubusercontent.com/b5008a5a5c4ae7ea9787bc87ff5e7b70b938bd305f93c1cce9939b319a5ad0f2/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230343430323233383338392e676966)
下拉刷新03-隐藏时间
// 隐藏时间
header.lastUpdatedTimeLabel.hidden = YES;
![(下拉刷新03-隐藏时间)](https://camo.githubusercontent.com/a66a5bc2d1643016fdf65e01809d6c7ae087267a437c9944c6c175f019fc78a2/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230343435363133323934342e676966)
下拉刷新04-隐藏状态和时间
// 隐藏时间
header.lastUpdatedTimeLabel.hidden = YES;
// 隐藏状态
header.stateLabel.hidden = YES;
![(下拉刷新04-隐藏状态和时间0)](https://camo.githubusercontent.com/ceaf7a075bd4ec2b6aeea8fc1b5a9ec224e60ae34240a83a9498fe15382259e7/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230343530383633393533392e676966)
下拉刷新05-自定义文字
// 设置文字
[header setTitle:@"Pull down to refresh" forState:MJRefreshStateIdle];
[header setTitle:@"Release to refresh" forState:MJRefreshStatePulling];
[header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
// 设置字体
header.stateLabel.font = [UIFont systemFontOfSize:15];
header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
// 设置颜色
header.stateLabel.textColor = [UIColor redColor];
header.lastUpdatedTimeLabel.textColor = [UIColor blueColor];
![(下拉刷新05-自定义文字)](https://camo.githubusercontent.com/c1021af1172a6271f203ebb60cd6398827e550c5661c61bcdced5649e7b88c29/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230343536333633333539332e676966)
下拉刷新06-自定义刷新控件
self.tableView.mj_header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
// 具体实现参考MJDIYHeader.h和MJDIYHeader.m
![(下拉刷新06-自定义刷新控件)](https://camo.githubusercontent.com/646590ef73d300989a4b5cee946f269fa9198e346e19ad54e0ded72167323db9/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353031393236313135392e676966)
上拉刷新01-默认
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
// 进入刷新状态后会自动调用这个block
}];
或
// 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadMoreData方法)
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
![(上拉刷新01-默认)](https://camo.githubusercontent.com/8189a18132456cfe0c8bf2daf5b5ab044529dc11b7473db5edeeb08cef3170f6/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353039303034373639362e676966)
上拉刷新02-动画图片
// 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadMoreData方法)
MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
// 设置刷新图片
[footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
// 设置尾部
self.tableView.mj_footer = footer;
![(上拉刷新02-动画图片)](https://camo.githubusercontent.com/94c2c8b02db84121f96d0cd59fba3b5127157bc47deda01e8fbdb645faa3052b/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353134313434353739332e676966)
上拉刷新03-隐藏刷新状态的文字
// 隐藏刷新状态的文字
footer.refreshingTitleHidden = YES;
// 如果没有上面的方法,就用footer.stateLabel.hidden = YES;
![(上拉刷新03-隐藏刷新状态的文字)](https://camo.githubusercontent.com/01dca5ed36ce471c56fe8c88613b0053ea162b8281e791cd69f0d7e881e0e626/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353230303938353737342e676966)
上拉刷新04-全部加载完毕
// 变为没有更多数据的状态
[footer endRefreshingWithNoMoreData];
![(上拉刷新04-全部加载完毕)](https://camo.githubusercontent.com/0150611ceee0f6ee06702d737adcf293cff946b80271a87335368f41d05743da/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353234383633343638362e676966)
上拉刷新05-自定义文字
// 设置文字
[footer setTitle:@"Click or drag up to refresh" forState:MJRefreshStateIdle];
[footer setTitle:@"Loading more ..." forState:MJRefreshStateRefreshing];
[footer setTitle:@"No more data" forState:MJRefreshStateNoMoreData];
// 设置字体
footer.stateLabel.font = [UIFont systemFontOfSize:17];
// 设置颜色
footer.stateLabel.textColor = [UIColor blueColor];
![(上拉刷新05-自定义文字)](https://camo.githubusercontent.com/941491408043fa74df162b1bab96a0caa835c2744a7eca31a4772d664f00a4f1/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353239353531313135332e676966)
上拉刷新06-加载后隐藏
// 隐藏当前的上拉刷新控件
self.tableView.mj_footer.hidden = YES;
![(上拉刷新06-加载后隐藏)](https://camo.githubusercontent.com/a941f0d6a19dfa8f3bfa24a692a3ec30261274c3f404df3b55ac309b5679e2bc/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353334333438313832312e676966)
上拉刷新07-自动回弹的上拉01
self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
![(上拉刷新07-自动回弹的上拉01)](https://camo.githubusercontent.com/ec1b2d59a8661404c432999fc3daa2b34cd87019243a0e4302d4ab5f457479ea/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353339323233393233312e676966)
上拉刷新08-自动回弹的上拉02
MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
// 设置普通状态的动画图片
[footer setImages:idleImages forState:MJRefreshStateIdle];
// 设置即将刷新状态的动画图片(一松开就会刷新的状态)
[footer setImages:pullingImages forState:MJRefreshStatePulling];
// 设置正在刷新状态的动画图片
[footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
// 设置尾部
self.tableView.mj_footer = footer;
![(上拉刷新07-自动回弹的上拉02)](https://camo.githubusercontent.com/c16657dac5dde57e9add7eb2596a0a2f9b89f21982dcf529f5c1ee3a90bcb747/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353434313434333632382e676966)
上拉刷新09-自定义刷新控件(自动刷新)
self.tableView.mj_footer = [MJDIYAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
// 具体实现参考MJDIYAutoFooter.h和MJDIYAutoFooter.m
![(上拉刷新09-自定义刷新控件(自动刷新))](https://camo.githubusercontent.com/4f2b2f714cabdc66d391414c011c7ce06e7b3d359af8893f6e19942207520fa9/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353530303139353836362e676966)
上拉刷新10-自定义刷新控件(自动回弹)
self.tableView.mj_footer = [MJDIYBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
// 具体实现参考MJDIYBackFooter.h和MJDIYBackFooter.m
![(上拉刷新10-自定义刷新控件(自动回弹))](https://camo.githubusercontent.com/24e42ee653dd85544cd129f60c5be76c70369b3490039119c50259143665fa09/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230353536303636363831392e676966)
UICollectionView01-上下拉刷新
// 下拉刷新
self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
// 进入刷新状态后会自动调用这个block
}];
// 上拉刷新
self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
// 进入刷新状态后会自动调用这个block
}];
![(UICollectionView01-上下拉刷新)](https://camo.githubusercontent.com/f7ddfa5b9eb1bcca0a676648c03cd9a8f9a159572132898954c4716637c6a2ea/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230363032313630333735382e676966)
UIWebView01-下拉刷新
// 添加下拉刷新控件
self.webView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
// 进入刷新状态后会自动调用这个block
}];
![(UICollectionView01-上下拉刷新)](https://camo.githubusercontent.com/88980452d1aca5c08761ce43a2cd5a5f4c339812952e825286f34329dc4151bc/687474703a2f2f696d61676573302e636e626c6f67732e636f6d2f626c6f67323031352f3439373237392f3230313530362f3134313230363038303531343532342e676966)
提醒
- 本框架纯ARC,兼容的系统>=iOS6.0、iPhone\iPad横竖屏
期待
- 如果在使用过程中遇到BUG,希望你能Issues我,谢谢(或者尝试下载最新的框架代码看看BUG修复没有)
- 如果在使用过程中发现功能不够用,希望你能Issues我,我非常想为这个框架增加更多好用的功能,谢谢
- 如果你想为MJRefresh输出代码,请拼命Pull Requests我
- 如果你开发的应用中用到了MJRefresh,希望你能到CocoaControls添加你应用的iTunes路径,我将会安装使用你的应用,并且根据众多应用的使用情况,对MJRefresh进行一个更好的设计和完善,提供更多好用的功能,谢谢
- 步骤01(微信是举个例子,百度“你的应用名称 itunes”)
![(step01)](https://camo.githubusercontent.com/66a2eac4e1c3803add0e12809c45e53eb397e9a651b8907dc573c3d74bf44fde/687474703a2f2f7777342e73696e61696d672e636e2f6d77313032342f3830306364663963747731657130766969763572736a3230736d3065613431742e6a7067)
- 步骤02
![(step02)](https://camo.githubusercontent.com/66579323302e9bc9a348b2e87b6f102c17de25d6c0be1372114f0ab4e168f71a/687474703a2f2f7777322e73696e61696d672e636e2f6d77313032342f383030636466396374773165713076696c656a786c6a32307475306d653761302e6a7067)
- 步骤03
![(step03)](https://camo.githubusercontent.com/abdd8d6bf9fd7ab083b6c49f4893d0d729f030e0125b55beb119fce0f0808e1b/687474703a2f2f7777312e73696e61696d672e636e2f6d77313032342f383030636466396374773165713076696f63706f356a3230776330646330756e2e6a7067)
- 步骤04
![(step04)](https://camo.githubusercontent.com/5cc43587b70d4f5f713dc11c1e33e52ce2e8827a8848a2246cae68b7ac594dfc/687474703a2f2f7777332e73696e61696d672e636e2f6d77313032342f3830306364663963747731657130766972313337786a323073693067657767752e6a7067)