简介
能给UITableview、UIcollectionView、UIScrollview提供上下拉刷新效果。
分类
- 下拉刷新类
MJRefreshNormalHeader (默认样式,有文字,上下箭头,菊花)
MJRefreshGifHeader (带动画效果)
- 上拉刷新类
MJRefreshAutoNormalFooter
MJRefreshAutoGifFooter (带动画效果)
MJRefreshBackNormalFooter(带回弹效果)
MJRefreshBackGifFooter (带回弹效果)
使用时,根据需要创建对应类型的对象
属性
下拉刷新类
NSString *lastUpdatedTimeKey; //保存上次刷新成功的时间
NSDate *lastUpdatedTime;
忽略头部,根据设定值的大小隐藏菊花
CGFloat ignoredScrollViewContentInsetTop;
设置初始时、下拉时、刷新时图片
[header setImages:Images forState:MJRefreshStateIdle];
[header setImages:Images forState:MJRefreshStatePulling];
[header setImages:Images forState:MJRefreshStateRefreshing];
设置标题
[header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
[header setTitle:@"松手刷新" forState:MJRefreshStatePulling];
[header setTitle:@"正在刷新数据" forState:MJRefreshStateRefreshing];
header.lastUpdatedTimeLabel.hidden = YES; // 隐藏时间label
header.stateLabel.hidden = YES; // 隐藏状态label
设置字体大小、颜色
header.stateLabel.font = [UIFont systemFontOfSize:15];
header.stateLabel.textColor = [UIColor redColor];
header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
header.lastUpdatedTimeLabel.textColor = [UIColor blueColor];
上拉刷新
忽略底部
CGFloat ignoredScrollViewContentInsetBottom;
自动显示或隐藏数据的计数(有数据-显示,没有数据-隐藏)
BOOL automaticallyHidden;
自动隐藏刷新(默认yes)
BOOL automaticallyRefresh;
当有很多控件在底部时自动刷新(默认是1,在控件的底部出现满,会自动刷新)
CGFloat triggerAutomaticallyRefreshPercent;
设置刷新时图片
[footer setImages:Images forState:MJRefreshStateRefreshing];
footer.stateLabel.hidden = YES;
[footer noticeNoMoreData]; //设为没有更多数据的状态
设置标题属性
[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];
加载完隐藏
self.tableView.mj_footer.hidden = YES;
方法
父类方法
-(void)beginRefreshing; //开始刷新
-(void)endRefreshing; //停止刷新
-(BOOL)isRefreshing; //正在刷新?
BOOL automaticallyChangeAlpha; //通过拖拽,自动改变透明度
下拉
/** NoticeNoMoreData */
-(void)noticeNoMoreData;
/** ResetNoMoreData(Clear the status of NoMoreData ) */
-(void)resetNoMoreData;
使用
导入
拖入框架文件夹到项目,import
创建
对应类的开始创建方法,有两种方式,一是带block,二是带监听方法(需另外定义)
- (instancetype)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock;
如
self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//当进入刷新状态时 自动调用,在此获取网络数据
}];
- (instancetype)headerWithRefreshingTarget:(id)target refreshingAction:(SEL)action;
MJRefreshNormalHeader *head = [MJRefreshNormalHeader headerWithRefreshingTarget:self
refreshingAction:@selector(refreshIt)];
//当进入刷新状态时 自动调用监听方法,在此获取网络数据
-
(instancetype)footerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock;
-
(instancetype)footerWithRefreshingTarget:(id)target refreshingAction:(SEL)action;
添加
把创建好的对象赋给控件(UITableview、UIcollectionView、UIScrollview)如:
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self
refreshingAction:@selector(refreshIt)];
self.tableView.mj_footer = footer;
封装使用
比较常用的是下拉刷新,上拉加载,于是将之封装为两个类方法,方便外界使用
在.h中
#import <Foundation/Foundation.h>
#import <MJRefresh.h>
@interface PDrefresher : NSObject
+(MJRefreshGifHeader *)setupDownPullRefresh_Header:(SEL)action target:(id)target;
+(MJRefreshAutoNormalFooter *)setupUpPullLoadingRefresh_Footer:(SEL)action target:(id)target;
@end
在.m中
+(MJRefreshGifHeader *)setupDownPullRefresh_Header:(SEL)action target:(id)target {
// 6.2下拉刷新器header
MJRefreshGifHeader *refreshHeader = [MJRefreshGifHeader headerWithRefreshingTarget:target refreshingAction:action];
NSArray *imgArr = [NSArray arrayWithObjects:
[UIImage imageNamed:@"动画1"],
[UIImage imageNamed:@"动画2"],
[UIImage imageNamed:@"动画3"],
[UIImage imageNamed:@"动画4"],
nil];
[refreshHeader setImages:imgArr forState:MJRefreshStatePulling];
[refreshHeader setImages:imgArr forState:MJRefreshStateRefreshing];
refreshHeader.lastUpdatedTimeLabel.hidden = YES; // 隐藏时间label
refreshHeader.stateLabel.hidden = YES; // 隐藏状态label
return refreshHeader;
}
+(MJRefreshAutoNormalFooter *)setupUpPullLoadingRefresh_Footer:(SEL)action target:(id)target{
// 6.1上滑刷新
MJRefreshAutoNormalFooter *refreshFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:target refreshingAction:action];
[refreshFooter setTitle:@"down to refrsh" forState:MJRefreshStateIdle];
refreshFooter.stateLabel.textColor = [UIColor redColor];
//refreshFooter.lastUpdatedTimeLabel.hidden = YES;
refreshFooter.stateLabel.hidden = YES;
refreshFooter.refreshingTitleHidden = YES;
refreshFooter.automaticallyChangeAlpha = YES; //上拉时透明度改变
return refreshFooter;
}
自定义
http://www.jb51.net/article/105107.htm
http://blog.csdn.net/json_6/article/details/51384260
http://www.cnblogs.com/wolfhous/p/6072626.html
网友评论