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);
}];}