当我们使用上拉或着下拉的时候,自己实现的话, 往往会耗费很多的时候来计算缩进。我们可以选择第三方的刷新控件,MJRefresh,这个也是现在使用的比较多的一个刷新,集成的话可以到github上面下载https://github.com/CoderMJLee/MJRefresh,也可以使用cocoapods集成 pod 'MJRefresh'
-
1 最简单添加方法 有状态,时间 默认颜色
UITableView *mytableView = [[UITableView alloc] initWithFrame:self.view.bounds];
mytableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
[self.view addSubview:mytableView];
- (void)headerAction
{
}
//手动进入刷新状态
[mytableView.mj_header beginRefreshing];
//手动结束刷新状态
[mytableView.mj_header endRefreshing];
//根据拖拽比例自动切换透明度
mytableView.mj_header.automaticallyChangeAlpha = YES;
//2使用block
mytableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//刷新代码
}];
-
下拉刷新有动画图片
我们在github下载框架,是可以动态展示刷新动画的,如果我们自己公司的项目,要提前准备好,三组动画图片,这样在不同的状态我们要显示,这里我演示了一下如何添加,并显示刷新状态的图片。图片资源可以从下载的MJRefresh例子找到!
//有动画图片
MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
//普通状态下的图片
NSMutableArray *idleImages = [NSMutableArray array];
for (int i = 0; i < 44; i++)
{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%d", i+1]];
NSLog(@"%@", [NSString stringWithFormat:@"dropdown_anim__000%02d", i+1]);
if (img) {
[idleImages addObject:img];
}
}
[header setImages:idleImages forState:MJRefreshStateIdle];
NSMutableArray *pullingImages = [NSMutableArray array];
for (int i = 45; i < 60; i++)
{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%d", i+1]];
NSLog(@"%@", [NSString stringWithFormat:@"dropdown_anim__000%d", i+1]);
if (img) {
[pullingImages addObject:img];
}
}
[header setImages:pullingImages forState:MJRefreshStatePulling];
//刷新d中的动画图片
NSMutableArray *refreshingImages = [NSMutableArray array];
for (int i = 0; i < 2; i++)
{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%i", i + 1]];
if (img) {
[refreshingImages addObject:img];
}
}
[header setImages:refreshingImages forState:MJRefreshStateRefreshing];
mytableView.mj_header = header;
如果我们想要控制是否显示时间之类
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
//是否隐藏时间
header.lastUpdatedTimeLabel.hidden = YES;
//文字颜色
header.stateLabel.textColor = [UIColor redColor];
//普通状态的文字
[header setTitle:@"下拽刷新" forState:MJRefreshStateIdle];
//松开可以刷新
[header setTitle:@"你刷开" forState:MJRefreshStatePulling];
//正在刷新
[header setTitle:@"刷新中sll" forState:MJRefreshStateRefreshing];
- 隐藏时间
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
header.lastUpdatedTimeLabel.hidden = YES;
mytableView.mj_header = header;
- 隐藏时间和状态
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
// //是否隐藏时间
header.lastUpdatedTimeLabel.hidden = YES;
//隐藏状态
header.stateLabel.hidden = YES;
mytableView.mj_header = header;
自定义文字
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
// //文字颜色
header.stateLabel.textColor = [UIColor redColor];
header.lastUpdatedTimeLabel.textColor = [UIColor blueColor];
//字体
header.stateLabel.font = [UIFont systemFontOfSize:15];
header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
//菊花的颜色
header.loadingView.color = [UIColor whiteColor];
// //普通
[header setTitle:@"往下拉刷新数据" forState:MJRefreshStateIdle];
//松开可以刷新
[header setTitle:@"松开加载数据" forState:MJRefreshStatePulling];
//正在刷新
[header setTitle:@"拼命加载中..." forState:MJRefreshStateRefreshing];
//文字慢慢的显现
mytableView.mj_header.automaticallyChangeAlpha = YES;
[self.view addSubview:mytableView];
mytableView.mj_header = header;
完全自定义内部刷新控件
这是我们创建的类要继承于MJRefreshHeader,可以根据里面设置状态的时候,处理我们自己的控件。
@implementation MyDiyRefreshHeader
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
/* 如果我们a在拖拽的时候希望上面要现实一张图片,广告,公司的标志等,可以在这个空间上面添加,只不过位置要改成负数的
*/
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor orangeColor];
label.hidden = YES;
[self addSubview:label];
self.label = label;
UIImageView *ad = [[UIImageView alloc] init];
ad.image = [UIImage imageNamed:@"dropdown_anim__00024"];
[self addSubview:ad];
self.adImageView = ad;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.label.frame = CGRectMake(0, 0, 220, 50);
self.adImageView.frame = CGRectMake((self.bounds.size.width -50) / 2, -50, 50, 50);
}
- (void)setState:(MJRefreshState)state
{
[super setState:state];
self.label.hidden = NO;
if (state == MJRefreshStateIdle) //正常
{
self.label.backgroundColor = [UIColor redColor];
}
else if (state == MJRefreshStatePulling) { //松开可以刷新
self.label.backgroundColor = [UIColor blueColor];
}
else if (state == MJRefreshStateRefreshing){ //正在刷新
self.label.backgroundColor = [UIColor orangeColor];
}
}
很方便的使用,我们不需要去关心滚动的计算了
//添加方法
self.tableView.mj_header = [MyDiyRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
//block方式添加
self.tableView.mj_header = [MyDiyRefreshHeader headerWithRefreshingBlock:^{
}];
上拉加载更多的使用
//默认方式
//block
// self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
// }];
//
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
//结束刷新
[mytableView.mj_footer endRefreshing];
//如果提醒他没有更多的数据了
[mytableView.mj_footer endRefreshingWithNoMoreData];
上拉带动画图片
//上拉加载带动画
MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
//刷新d中的动画图片
NSMutableArray *refreshingImages = [NSMutableArray array];
for (int i = 0; i < 2; i++)
{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%i", i + 1]];
if (img) {
[refreshingImages addObject:img];
}
}
[footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
self.tableView.mj_footer = footer;
上拉隐藏刷新的文字
MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
//刷新d中的动画图片
NSMutableArray *refreshingImages = [NSMutableArray array];
for (int i = 0; i < 2; i++)
{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%i", i + 1]];
if (img) {
[refreshingImages addObject:img];
}
}
[footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
//隐藏刷新的文字
footer.refreshingTitleHidden = YES;
self.tableView.mj_footer = footer;
上拉全部加载完毕
//全部加载完毕
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
//刷新结束的时候要调用
[self.tableView.mj_footer endRefreshingWithNoMoreData];
上拉自定义文字
/上拉自定义文字
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
[footer setTitle:@"点击或拖拽加载" forState:MJRefreshStateIdle];
[footer setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
[footer setTitle:@"没有更多数据" forState:MJRefreshStateNoMoreData];
// Set font
footer.stateLabel.font = [UIFont systemFontOfSize:17];
// Set textColor
footer.stateLabel.textColor = [UIColor blueColor];
self.tableView.mj_footer = footer;
上拉自动回弹在最底部
self.tableView.tableFooterView = [[UIView alloc] init];
self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
上拉加载回弹带图片动画
//自动回弹2带h动画图片
MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
// 普通状态下的图片
NSMutableArray *idleImages = [NSMutableArray array];
for (int i = 0; i < 44; i++)
{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%d", i+1]];
NSLog(@"%@", [NSString stringWithFormat:@"dropdown_anim__000%02d", i+1]);
if (img) {
[idleImages addObject:img];
}
}
[footer setImages:idleImages forState:MJRefreshStateIdle];
// 松开刷开的时候
NSMutableArray *pullingImages = [NSMutableArray array];
for (int i = 45; i < 60; i++)
{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%d", i+1]];
NSLog(@"%@", [NSString stringWithFormat:@"dropdown_anim__000%d", i+1]);
if (img) {
[pullingImages addObject:img];
}
}
[footer setImages:pullingImages forState:MJRefreshStatePulling];
// 刷新的图片
NSMutableArray *refreshingImages = [NSMutableArray array];
for (int i = 0; i < 2; i++)
{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%i", i + 1]];
if (img) {
[refreshingImages addObject:img];
}
}
[footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
// Set footer
self.tableView.mj_footer = footer;
self.tableView.tableFooterView = [[UIView alloc] init];
自定义刷新控件
#import "MyDIYAutoFooter.h"
@interface MyDIYAutoFooter ()
@property (nonatomic, weak) UILabel *label;
@property (nonatomic, weak) UIActivityIndicatorView *loading;
@end
@implementation MyDIYAutoFooter
#pragma mark - 添加子控件 -
- (void)prepare
{
[super prepare];
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor orangeColor];
label.text = @"自定义的刷新";
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
self.label = label;
UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self addSubview:loading];
self.loading = loading;
}
#pragma mark - 设置自控件的尺寸和位子 -
- (void)placeSubviews
{
[super placeSubviews];
self.label.frame = CGRectMake((self.bounds.size.width - 200) / 2, 0, 200, 50);
self.loading.center = CGPointMake(35, 25);
}
#pragma mark - 监听状态的改变 -
- (void)setState:(MJRefreshState)state
{
MJRefreshCheckState;
switch (state) {
case MJRefreshStateIdle:
self.label.text = @"拖动加载更多";
[self.loading stopAnimating];
break;
case MJRefreshStatePulling:
self.label.text = @"松开加载更多";
[self.loading stopAnimating];
break;
case MJRefreshStateRefreshing:
self.label.text = @"加载数据中!!!";
[self.loading startAnimating];
break;
case MJRefreshStateNoMoreData:
self.label.text = @"没有更多的数据!!!";
[self.loading stopAnimating];
break;
default:
break;
}
}
使用
self.tableView.mj_footer = [MyDIYAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
上拉自动回弹,自定义视图
#import "MJRefreshBackFooter.h"
@interface MyDIYBackFooter : MJRefreshBackFooter
@end
#import "MyDIYBackFooter.h"
@interface MyDIYBackFooter ()
@property (nonatomic, weak) UILabel *label;
@property (nonatomic, weak) UIActivityIndicatorView *loading;
@end
@implementation MyDIYBackFooter
#pragma mark - 添加子控件 -
- (void)prepare
{
[super prepare];
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor orangeColor];
label.text = @"自定义的刷新";
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
self.label = label;
UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self addSubview:loading];
self.loading = loading;
}
#pragma mark - 设置自控件的尺寸和位子 -
- (void)placeSubviews
{
[super placeSubviews];
self.label.frame = CGRectMake((self.bounds.size.width - 200) / 2, 0, 200, 50);
self.loading.center = CGPointMake(35, 25);
}
#pragma mark - 监听状态的改变 -
- (void)setState:(MJRefreshState)state
{
MJRefreshCheckState;
switch (state) {
case MJRefreshStateIdle:
self.label.text = @"拖动加载更多";
[self.loading stopAnimating];
break;
case MJRefreshStatePulling:
self.label.text = @"松开加载更多";
[self.loading stopAnimating];
break;
case MJRefreshStateRefreshing:
self.label.text = @"加载数据中!!!";
[self.loading startAnimating];
break;
case MJRefreshStateNoMoreData:
self.label.text = @"没有更多的数据!!!";
[self.loading stopAnimating];
break;
default:
break;
}
}
@end
//使用
self.tableView.mj_footer = [MyDIYBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
self.tableView.tableFooterView = [[UIView alloc] init];
如果我们的刷新控件自已的很多地方都需要使用,需要对刷新控件进行调整,需要自己定义一个类来完成操作
//是否隐藏时间
header.lastUpdatedTimeLabel.hidden = YES;
//文字颜色
header.stateLabel.textColor = [UIColor redColor];
//文字颜色
header.stateLabel.textColor = [UIColor redColor];
//普通
[header setTitle:@"下拽刷新" forState:MJRefreshStateIdle];
//松开可以刷新
[header setTitle:@"你刷开" forState:MJRefreshStateIdle];
//正在刷新
[header setTitle:@"刷新中sll" forState:MJRefreshStateIdle];
如果很多的地方都需要使用到这些属性的人话,我们可以自己定义一个类,让他继承于MJRefreshNormalHeader
#import "MyRefreshHeader.h"
@implementation MyRefreshHeader
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
//是否隐藏时间
self.lastUpdatedTimeLabel.hidden = YES;
//文字颜色
self.stateLabel.textColor = [UIColor redColor];
//普通
[self setTitle:@"下拽刷新" forState:MJRefreshStateIdle];
//松开可以刷新
[self setTitle:@"你刷开" forState:MJRefreshStatePulling];
//正在刷新
[self setTitle:@"刷新中sll" forState:MJRefreshStateRefreshing];
return self;
}
}
@end