首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS 判斷 UITableView 是否滾動至頂部:contentOffset.y 為 0 即頂部

     編輯:程式設計綜合問答

判斷邏輯

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:。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved