MJRefresh 类结构图:
MIRefreshComponent:刷新控件的基类
MJRefreshHeader:基础的下拉刷新控件(Header)
MJRefreshStateHeader:带有状态文字的下拉刷新控件
MJRefreshNormalHeader:默认的下拉刷新控件
MJRefreshGitHeader:带动图的下拉刷新控件
MJRefreshFooter:基础的下拉刷新控件(Footer)
MJRefreshBackFooter:会回弹到底部的下拉刷新控件
MJRefreshAutoFooter:会自动刷新的上拉刷新控件
![](/qrcode.jpg)
MJRefreshBackStateFooter:带有状态文字的上拉加载控件MJRefreshAutoStateFooter:带有状态文字的上拉加载控件
MIRefreshBackNormalFooter:默认的上拉加载控件
MIRefreshBackGifFooter:带动图的上拉加载控件
MIRefreshAutoNormalFooter:默认的上拉加载控件
MIRefreshAutoGifFooter:带动图的上拉加载控件
使用:
+ (MJRefreshGifHeader *)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock{
MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:refreshingBlock];
header.lastUpdatedTimeLabel.hidden = YES;
header.stateLabel.hidden = YES;
NSMutableArray *a = @[].mutableCopy;
for (int i =0 ; i<4; i++) {
[a addObject:[UIImage imageNamed:[NSString stringWithFormat:@"下啦刷新-%d",i+1]]];
}
[header setImages:a duration:0.5f forState:MJRefreshStateRefreshing];
[header setImages:@[[UIImage imageNamed:@"松手加载-1"],[UIImage imageNamed:@"松手加载-2"]] duration:0.3f forState:MJRefreshStatePulling];
[header setImages:@[[UIImage imageNamed:@"用力一点-1"],[UIImage imageNamed:@"用力一点-2"]] duration:0.5f forState:MJRefreshStateIdle];
header.backgroundColor = [UIColor clearColor];
return header;
}
+ (MJRefreshAutoNormalFooter *)footerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock{
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:refreshingBlock];
footer.backgroundColor = [UIColor clearColor];
footer.automaticallyRefresh = YES;
footer.triggerAutomaticallyRefreshPercent = -3.0;
return footer;
}
_collectionView = [[XBHomeCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
_collectionView.homeDelegate = self;
_collectionView.backgroundColor = kBlackColor;
XBWeakSelf
_collectionView.mj_header = [XBTool headerWithRefreshingBlock:^{
XBStrongSelf
[self reloadData:NO];
}];
_collectionView.mj_footer = [XBTool footerWithRefreshingBlock:^{
XBStrongSelf
[self reloadData:YES];
}];
[self.view addSubview:_collectionView];
self.collectionView.mj_footer.hidden = YES;
-(void)reloadData:(BOOL)append {
if (!append) {
self.currentPage = 1;
[_dataArray removeAllObjects];
}else {
self.currentPage += 1;
}
[self getData:append];
}
- (void)getData:(BOOL)append{
XBWeakSelf
//获取精选列表
NSDictionary * dic = @{@"currentPage":[NSString stringWithFormat:@"%ld",(long)self.currentPage],
@"pageSize":[NSString stringWithFormat:@"%ld",(long)DefaultPageSize]
};
[[HKNetService jsonManager] sendPOSTWithUrl:SERVER_HOMEHANDPICKLIST params:dic success:^(HKURLResponse *response, HKResultMessage *resultMessage) {
XBStrongSelf
NSDictionary * dic = response.result;
NSArray * array = [dic valueForKey:@"data"];
for (NSDictionary *dic in array) {
XBHomeModel * model = [[XBHomeModel alloc] init];
[model setValuesForKeysWithDictionary:dic];
[self.dataArray addObject:model];
}
if ([response.responseData isKindOfClass:[NSDictionary class]] || [response.responseData isKindOfClass:[NSArray class]]) {
NSString *b = [response.responseData JF_jsonString];
[XBUserDefaults setObject:b forKey:HandpickListData];
[XBUserDefaults synchronize];
}
[self endRefreshing];
if (self.dataArray.count) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView setDataArray:self.dataArray];
});
}
if (array.count<DefaultPageSize) {
self.collectionView.mj_footer.hidden = YES;
}
if (array.count == 0) {
[self.collectionView.mj_footer endRefreshingWithNoMoreData];
}
} failure:^(HKURLResponse *response, HKResultMessage *resultMessage) {
XBStrongSelf
[self endRefreshing];
[HKToast showErrorMessage:resultMessage];
}];
}
- (void)endRefreshing {
[self.collectionView.mj_header endRefreshing];
[self.collectionView.mj_footer endRefreshing];
}
下拉刷新-动画图片
// 设置回调(一旦进入刷新状态,就调用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;