在動手實現之前,先把 UIScrollView 的三個關鍵屬性搞清楚,後面的原理就是它們的排列組合。
contentOffset.y 就是 480,表示內容向上偏移了 480。核心思路是監測 contentOffset.y 的值。當用戶往下拉動時,contentOffset.y 會變成負數並持續減小。一旦越過閾值,就用動畫把 contentInset.top 撐開到一個定值(騰出刷新指示器的位置),隨後通過延時把 inset 恢復原值。
//下拉刷新
if (scrollView.contentOffset.y < -100) {
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
} completion:^(BOOL finished) {
NSLog(@"發起下拉刷新");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}];
});
}];
}
原理與下拉刷新幾乎完全一致,只是判斷條件不同。當 contentSize.height 減小於「可見高度 + 當前偏移」時,說明用戶已經拉到底部,觸發上拉加載。
//上拉加載
if (scrollView.bounds.size.height + scrollView.contentOffset.y > scrollView.contentSize.height) {
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
} completion:^(BOOL finished) {
NSLog(@"發起上拉加載");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}];
});
}];
}
#import "ViewController.h"
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation ViewController
- (UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//contentSize是可滑動的區域
_scrollView.contentSize = CGSizeMake(0, 900);
_scrollView.backgroundColor = [UIColor grayColor];
_scrollView.delegate = self;
//greenView實際上就是UIScrollView的content View
UIView *greenView = [[UIView alloc] initWithFrame:
CGRectMake(0, 0, self.view.bounds.size.width, 900)];
greenView.backgroundColor = [UIColor greenColor];
[_scrollView addSubview:greenView];
}
return _scrollView;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
//下拉刷新-當下拉刷新時,contentOffset實際上就是greenView相對於屏幕左上角的偏移量
if (scrollView.contentOffset.y < -100) {
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
} completion:^(BOOL finished) {
NSLog(@"發起下拉刷新");
//設置延時時間為2秒
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1.0 animations:^{
//恢復之前的contentInset,讓greenView回到原來的地方
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}];
});
}];
}
//上拉加載
if (scrollView.bounds.size.height + scrollView.contentOffset.y > scrollView.contentSize.height) {
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
} completion:^(BOOL finished) {
NSLog(@"發起上拉加載");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}];
});
}];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.scrollView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
1. 為什麼選在 scrollViewWillBeginDecelerating 中判斷
這個方法在用戶「松手、列表開始減速」的那一刻觸發,此時 contentOffset 已經到達最終位置,是判斷用戶意圖的最佳時機。如果在 scrollViewDidScroll 中判斷,會在拖動過程中反覆觸發,導致動畫錯亂。
2. contentInset 是撐開空間的關鍵
下拉時把 top 撐到 100,等於在內容頂部硬生生插入一段空白,用來安放菊花或提示文案。刷新結束後把 inset 歸零,空白消失,內容自然回位。整個過程不需要改變 contentSize,也不會影響內容本身的布局。
3. 動畫時長的取捨
示例中用了 1 秒的動畫,實際項目裡 0.2 到 0.3 秒手感更好。示例中的 2 秒延時是模擬網絡請求,真實場景應替換為請求完成後的 completion 回調。
如果只是想盡快用上成熟穩定的功能,可以直接看 GitHub 上的 MJRefresh,它把上述原理封裝得非常完整,支持下拉動畫、多種刷新樣式、自動刷新、越界判斷等豐富場景,是國內 iOS 社區的事實標準。
但理解原理仍然重要:出現「刷新坐標不對」「iOS 11 之後 adjustedContentInset 導致位置偏移」「刷新與 keyboard 同時出現時跳動」這類問題時,只有懂原理才能快速定位。順帶一提,iOS 11 引入的 adjustedContentInset 會自動疊加安全區域,取值時更推薦讀它而不是 contentInset。