ios 检测是否已到达UIScrollview底部

6g8kf2rb  于 2022-12-20  发布在  iOS
关注(0)|答案(9)|浏览(211)

我有一个带有图片的UIScrollView,当用户滚动到滚动视图的末尾时,我想更新内容。这是我的代码,用于检测何时到达滚动视图的底部:
下面的代码是在scrollViewDidScroll:委托中实现的

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,但它不起作用,函数没有被调用

jm2pwxwz

jm2pwxwz1#

如果您想在swift中检测它们:

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
}}
iqxoj9l9

iqxoj9l92#

卡洛斯的回答更好。
对于Swift 4.x,您必须更改方法名称:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
            //bottom reached
        }
    }
btxsgosb

btxsgosb3#

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

ldfqzlk84#

有时候你必须在条件中使用+1,因为contentSize.height会多给你几位小数,所以如果你使用这个,你就避免了...

override func scrollViewDidScroll(scrollView: UIScrollView) {

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

oknrviil5#

你有没有想过添加一个布尔值。当方法第一次被调用时更新它,或者当用户向上滚动时更新它。

6jjcrrmo

6jjcrrmo6#

我使用了一种混合的方法,让我解释一下:
虽然您的演算是正确的,但是您所监听的委托是多余的,因为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
    }
}
pgvzfuti

pgvzfuti7#

对于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
    }      
}
2nbm6dog

2nbm6dog8#

实现scrollViewDidScroll:并在中检查contentOffset,以达到终点

q9yhzks0

q9yhzks09#

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

相关问题