標籤:
一.下載第三方庫
https://github.com/samvermette/SVPullToRefresh
二.使用方法
1.匯入下載的第三方庫
2.匯入標頭檔
在需要的類裡匯入這一個標頭檔就能,同時使用下拉重新整理,上拉載入
#import "SVPullToRefresh.h"
3.下拉重新整理
[objc] view plain copy
- [_tableView addPullToRefreshWithActionHandler:^{
- //資料的載入,和表的重新整理的代碼。
- //全部需要我們自己寫
- // [self refreshTableView];
- //3秒後調用refreshTableView方法
- [self performSelector:@selector(refreshTableView) withObject:nil afterDelay:1.0];
- // //風火輪的動畫還需要我們手動的停止
- // [_tableView.pullToRefreshView stopAnimating];
- }];
4.上拉載入
[objc] view plain copy
- [_tableView addInfiniteScrollingWithActionHandler:^{
- [self performSelector:@selector(insertDate) withObject:nil afterDelay:3.0];
- }];
5.自訂顯示的文字
[objc] view plain copy
- [_tableView.pullToRefreshView setTitle:@"拖動以重新整理" forState:SVPullToRefreshStateTriggered];
- [_tableView.pullToRefreshView setTitle:@"重新整理完了呀" forState:SVPullToRefreshStateStopped];
- [_tableView.pullToRefreshView setTitle:@"不要命的載入中..." forState:SVPullToRefreshStateLoading];
6.自訂重新整理時的動畫
[objc] view plain copy
- UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
- view.backgroundColor = [UIColor blueColor];
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:3];
- view.alpha = 0.5 ;
- [UIView commitAnimations];
- [_tableView.pullToRefreshView setCustomView:view forState:SVPullToRefreshStateAll];
當然可以把view換成imageView等控制項實現動畫效果
7.實現重新整理和載入的兩個方法
[objc] view plain copy
- - (void)refreshTableView
- {
- }
- - (void)insertDate
- {
- }
三.注意事項
iOS7和我們的下拉重新整理庫有衝突,因為都是對contentOffset進行操作,解決衝突,有兩種做法
方法1:禁用navgationbar的半透明效果,還原至iOS6中的效果
[objc] view plain copy
- self.navigationController.navigationBar.translucent = NO;
方法2:禁止系統自己修改contentOffset,然後修改tableView的frame
automaticallyAdjustsScrollViewInsets 這個屬性,只有iOS7以後才能用,所以如果要相容iOS6,在iOS6運行,我們需要判斷一下能不能使用這個屬性或者當前的系統版本
最終代碼為:
[objc] view plain copy
- if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
[objc] view plain copy
- {
- self.automaticallyAdjustsScrollViewInsets = NO;
-
- _tableView.frame = CGRectMake(0, 64, 320, self.view.bounds.size.height - 64);
- };
SVPullToRefresh? 下拉重新整理,上拉載入