判斷邏輯
if (tableView.contentOffset.y <= 0) {
// 已在頂部(或超出彈性區域)
NSLog(@"TableView at top");
} else {
// 未在頂部
NSLog(@"TableView offset: %f", tableView.contentOffset.y);
}
應用場景:滾動動畫結束後執行動作
- (void)scrollToTopAndDoSomething {
if (tableView.contentOffset.y > 0) {
// 需要滾動,設定 delegate 監聽動畫結束
[tableView setContentOffset:CGPointZero animated:YES];
// 需實作 scrollViewDidEndScrollingAnimation:
} else {
// 已在頂部,直接執行
[self doSomething];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[self doSomething];
}
注意
contentOffset.y == 0 表示頂部;若有 contentInset.top,頂部對應 -contentInset.top。
scrollViewDidEndScrollingAnimation: **僅在程式碼呼叫 setContentOffset:animated:YES 時觸發**,使用者手勢滾動結束走 scrollViewDidEndDecelerating:。