簡體   English   中英

檢測到 UIScrollview 底部

[英]Detect UIScrollview bottom reached

我有一個帶有圖像的 UIScrollView,當用戶滾動到滾動視圖的末尾時,我想更新內容。 這是我檢測何時到達滾動視圖底部的代碼:

下面的代碼在scrollViewDidScroll: delegate 中實現

CGFloat scrollViewHeight = imagesScrollView.bounds.size.height;
CGFloat scrollContentSizeHeight = imagesScrollView.contentSize.height;
CGFloat bottomInset = imagesScrollView.contentInset.bottom;
CGFloat scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight;

if(imagesScrollView.contentOffset.y > scrollViewBottomOffset){


    [imagesView addSubview:imagesBottomLoadingView];

    [self downloadImages];

}

我的問題是,當用戶滾動到底部時,我的函數被調用了幾次,但我只想調用它一次。 我嘗試使用 imagesScrollView.contentOffset.y == scrollViewBottomOffset 但它不起作用並且未調用該函數

如果您想快速檢測它們:

override func scrollViewDidScroll(scrollView: UIScrollView) {

if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
    //reach bottom
}

if (scrollView.contentOffset.y < 0){
    //reach top
}

if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y < (scrollView.contentSize.height - scrollView.frame.size.height)){
    //not top and not bottom
}}

卡洛斯的回答更好。

對於 Swift 4.x,您必須更改方法名稱:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
            //bottom reached
        }
    }
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
    float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (bottomEdge >= scrollView.contentSize.height) 
    {
        // we are at the end
    }
}

你有沒有想過添加一個布爾值。 在第一次調用該方法時更新它,也許當用戶向上滾動時更新它。

有時您將不得不在條件中使用 +1,因為 contentSize.height 給了您幾個小數,所以如果您使用它,您可以避免它......

override func scrollViewDidScroll(scrollView: UIScrollView) {

   if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
       //bottom reached
   }
}

實現scrollViewDidScroll:並檢查contentOffset以達到終點

對於 Swift 4.5:

func scrollViewDidScroll(_ scrollView: UIScrollView) 
{    
   let scrollViewHeight = scrollView.frame.size.height
    let scrollContentSizeHeight = scrollView.contentSize.height
     let scrollOffset = scrollView.contentOffset.y

    if (scrollOffset == 0)
    {
        // then we are at the top
        print("then we are at the top")
    }
    else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
    {
        print("then we are at the end")
        // then we are at the end
    }      
}

我為此使用了混合方法。 讓我解釋:

雖然您的微積分是正確的,但您收聽的委托是一種矯枉過正,因為scrollViewDidScroll被多次調用,這可能會導致性能問題。 您應該(像我一樣)使用scrollViewDidEndDraggingscrollViewDidEndDecelerating ,它們僅在每個事件結束時調用一次。

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    // Scrolling acceleration didn't continue after the finger was lifted
    if !decelerate {
        executeActionAtTheEnd(of: scrollView)
    }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    executeActionAtTheEnd(of: scrollView)
}

private func executeActionAtTheEnd(of scrollView: UIScrollView) {
    if scrollView.contentOffset.y + 1 >= (scrollView.contentSize.height - scrollView.frame.size.height) {
        // Do here whatever you want when end of scrolling is reached
    }
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
    //reach bottom
}

if (scrollView.contentOffset.y <= 0){
    //reach top
}

if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y < (scrollView.contentSize.height - scrollView.frame.size.height)){
    //not top and not bottom
}}

確保您的視圖實現了 UIScrollViewDelegate

MyView: UITableViewDelegate,UIScrollViewDelegate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM