UITableview下拉刷新部分
自定义View 显示UIActivityIndicatorView加载动画
self.tableViewHeader= [[CustomViewHeader alloc]
initWithFrame:CGRectMake(0, -40,self.view.bounds.size.width,40)];
[self.tableView addSubview:_tableViewHeader];//加载tableview上
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
//判断是否是下拉操作,是否正在loading
if(scrollView.contentOffset.y< -15.0f&& !_isLoading) {
_isLoading=YES;//设置正在loading状态
DebugLog(@"contentOffset.y:%.2f", scrollView.contentOffset.y);
[self.tableViewHeader startAnimating];//开始动画
UIEdgeInsets edgeInset = self.tableView.contentInset;
self.tableView.contentInset=UIEdgeInsetsMake(44.0f,edgeInset.left,edgeInset.bottom,edgeInset.right);
//重发请求
[self performSelector:@selector(getData) withObject:nil afterDelay:0.4f];
}}
获取数据方法
- (void)getData {
...
[self stopHeaderRefresh];//获取数据结束 停止动画
}
停止动画的方法
- (void)stopHeaderRefresh {
_isLoading=NO;//设置不在loading状态
[self.tableViewHeader stopAnimating];
[UIViewanimateWithDuration:0.25fanimations:^{
UIEdgeInsetsedgeInset =self.tableView.contentInset;
self.tableView.contentInset=UIEdgeInsetsMake(0.0, edgeInset.left, edgeInset.bottom, edgeInset.right);
}];
}
UITableview上拉加载部分
自定义View 显示UIActivityIndicatorView加载动画
self.tableViewFooter= [[CustomViewHeader alloc]
initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,40)];
//加在tableviewFooterViewController上
[self.tableView.tableFooterView addSubview:self.tableViewFooter];
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
//是否是上拉操作
BOOL isPull = (scrollView.contentSize.height>CGRectGetHeight(self.tableView.frame))&&(scrollView.contentSize.height-scrollView.contentOffset.y<CGRectGetHeight(self.tableView.frame));
//_isLoading是否正在loading,_isNeedLoading是否需要loading(业务需求)
if(isPull && !_isLoading&&_isNeedLoading) {
_isLoading=YES;//设置正在loading的状态
[self.tableViewFooter startAnimating];
UIEdgeInsets edgeInset = self.tableView.contentInset;
self.tableView.contentInset=UIEdgeInsetsMake(edgeInset.top,edgeInset.left,0.0f,edgeInset.right);
//获取更多数据
[self performSelector:@selector(getMoreData) withObject:nil afterDelay:0.4f];
}}
获取更多数据方法
- (void)getMoreData {
...
[self stopFooterRefresh];//获取数据结束 停止动画
}
停止动画的方法
- (void)stopFooterRefresh {
_isLoading=NO;//设置不在loading状态
[self.tableViewFooter stopAnimating];//停止动画
__weak typeof(self) weakSelf = self;
[UIViewanimateWithDuration:0.25fanimations:^{
UIEdgeInsetsedgeInset = weakSelf.tableView.contentInset;
weakSelf.tableView.contentInset=UIEdgeInsetsMake(edgeInset.top, edgeInset.left, -44.0f, edgeInset.right);
}];}