.h 文件
#import <UIKit/UIKit.h>
@protocol RefreshTableFooterViewDelegate;
@interface RefreshTableFooterView : UIView
@property (nonatomic, weak) id<RefreshTableFooterViewDelegate> delegate;
// 实现ScrollViewDelegate 后在对应的 didEndingDragging 方法中调用
-(void)refreshFooterScrollViewDidEndDragging:(UIScrollView *)scrollView;
// 在重新刷新 tableView 后调用
-(void)refreshFooterViewDataSourceDidFinishedLoading:(UIScrollView*)scrollView;
// 在重新刷新 tableView 之前调用
-(void)refreshFooterViewShouldBeHiddenBeforeDataSourceReloadNewData;
@end
@protocol RefreshTableFooterViewDelegate <NSObject>
-(void)refreshTableFooterViewTriggerRefresh;
-(BOOL)refreshTableFooterViewIsLoading;
-(BOOL)refreshTableFooterViewHasMore;
@end
.m 文件
#import "RefreshTableFooterView.h"
@implementation RefreshTableFooterView
{
UIActivityIndicatorView *_indicatorView;
UILabel *_descriptionLabel;
BOOL _isLastPage;
BOOL _hasRefreshed;
}
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
CGFloat yAxis = 3;
CGFloat width = 200;
_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_indicatorView.frame = CGRectMake((CGRectGetWidth(self.bounds) - 200)/2 - CGRectGetWidth(_indicatorView.bounds), yAxis, CGRectGetWidth(_indicatorView.bounds), CGRectGetHeight(_indicatorView.bounds));
_indicatorView.hidesWhenStopped = YES;
[self addSubview:_indicatorView];
[_indicatorView stopAnimating];
_descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.bounds) - 200)/2, yAxis, width, CGRectGetHeight(_indicatorView.bounds))];
_descriptionLabel.text = @"上拉刷新";
_descriptionLabel.textAlignment = NSTextAlignmentCenter;
_descriptionLabel.textColor = [UIColor lightGrayColor];
[self addSubview:_descriptionLabel];
}
return self;
}
-(void)refreshFooterScrollViewDidEndDragging:(UIScrollView *)scrollView
{
if (!self.delegate ||
![self.delegate respondsToSelector:@selector(refreshTableFooterViewIsLoading)] ||
[self.delegate refreshTableFooterViewIsLoading]) {
return;
}
if (![self.delegate refreshTableFooterViewHasMore]) {
_descriptionLabel.text = @"已经加载所有~";
return;
}
CGFloat contentHeight = scrollView.contentSize.height;
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat containerHeight = scrollView.bounds.size.height;
BOOL refreshOrNot = containerHeight + offsetY > contentHeight - 0;
if (refreshOrNot && [self.delegate respondsToSelector:@selector(refreshTableFooterViewTriggerRefresh)]) {
[self.delegate refreshTableFooterViewTriggerRefresh];
_descriptionLabel.text = @"加载中...";
_indicatorView.hidden = NO;
[_indicatorView startAnimating];
[scrollView setContentInset:UIEdgeInsetsMake(.0, 50.0, .0, .0)];
}
}
-(void)refreshFooterViewShouldBeHiddenBeforeDataSourceReloadNewData {
self.hidden = YES;
}
-(void) refreshFooterViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {
[scrollView setContentInset:UIEdgeInsetsMake(.0, .0, .0, .0)];
CGRect original = self.frame;
original.origin.y = scrollView.contentSize.height > scrollView.bounds.size.height ? scrollView.contentSize.height : scrollView.bounds.size.height;
self.frame = original;
self.hidden = NO;
[_indicatorView stopAnimating];
_descriptionLabel.text = @"上拉刷新";
}
@end