UIScrollView,到达滚动视图的底部

发布于 2024-11-13 08:43:20 字数 286 浏览 1 评论 0原文

我知道Apple文档有以下委托方法:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // called when scroll view grinds to a halt

但是,这并不一定意味着您处于底层。因为如果你用手指滚动一点,然后它会减速,但你实际上并不在滚动视图的底部,那么它仍然会被调用。我基本上想要一个箭头来显示我的滚动视图中有更多数据,然后当您位于底部时(就像它弹跳时)消失。谢谢。

I know the Apple documentation has the following delegate method:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // called when scroll view grinds to a halt

However, it doesn't necessarily mean you are at the bottom. Cause if you use your finger, scroll a bit, then it decelerates, but you are not actually at the bottom of your scroll view, then it still gets called. I basically want an arrow to show that there is more data in my scroll view, and then disappear when you are at the bottom (like when it bounces). Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

み零 2024-11-20 08:43:20

我认为您可以做的是检查您的 contentOffset 点是否位于 contentSize 的底部。因此,您可能会这样做:

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

您可能还需要一个负面案例,以便在用户向上滚动时显示您的指示器。您可能还想添加一些填充,例如,当用户靠近底部但不完全位于底部时,您可以隐藏指示器。

I think what you might be able to do is to check that your contentOffset point is at the bottom of contentSize. So you could probably do something like:

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

You'll likely also need a negative case there to show your indicator when the user scrolls back up. You might also want to add some padding to that so, for example, you could hide the indicator when the user is near the bottom, but not exactly at the bottom.

迷途知返 2024-11-20 08:43:20

因此,如果您想要快速完成,请执行以下操作:

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

So if you want it in swift, here you go:

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
    }
}
溺深海 2024-11-20 08:43:20

我认为 @bensnider 的答案是正确的,但不是 exart。由于这两个原因,

1. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{}

如果我们检查 if (bottomEdge >=scrollView.contentSize.height)

2 , 该方法将连续调用。在此,如果我们选择 == 检查此条件将有效两次。

  • (i) 当滚动视图的末端触及底部边缘时,我们将向上滚动
  • (ii) 当滚动视图弹回以保留其自身位置时

我觉得这样更准确。

很少有情况这个条件也有效两次。但用户不会遇到这种情况。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
 {
    if (scrollView.contentOffset.y == roundf(scrollView.contentSize.height-scrollView.frame.size.height)) {
    NSLog(@"we are at the endddd");

   //Call your function here...

    }
}

I Think @bensnider answer is correct, But not exart. Because of these two reasons

1. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{}

This method will call continuously if we check for if (bottomEdge >= scrollView.contentSize.height)

2 . In this if we go for == check also this condition will valid for two times.

  • (i) when we will scroll up when the end of the scroll view touches the bottom edge
  • (ii) When the scrollview bounces back to retain it's own position

I feel this is more accurate.

Very few cases this codition is valid for two times also. But User will not come across this.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
 {
    if (scrollView.contentOffset.y == roundf(scrollView.contentSize.height-scrollView.frame.size.height)) {
    NSLog(@"we are at the endddd");

   //Call your function here...

    }
}
耀眼的星火 2024-11-20 08:43:20

仅当底部 contentInset 值非负时,接受的答案才有效。稍微的演变将考虑 contentInset 的底部,无论其符号如何:

CGFloat bottomInset = scrollView.contentInset.bottom;
CGFloat bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height - bottomInset;
if (bottomEdge == scrollView.contentSize.height) {
    // Scroll view is scrolled to bottom
}

The accepted answer works only if the bottom contentInset value is non-negative. A slight evolution would consider the bottom of the contentInset regardless of it's sign:

CGFloat bottomInset = scrollView.contentInset.bottom;
CGFloat bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height - bottomInset;
if (bottomEdge == scrollView.contentSize.height) {
    // Scroll view is scrolled to bottom
}
方觉久 2024-11-20 08:43:20

实际上,这段代码(用 Swift 3 编写)在性能方面不只是将 @bensnider 的代码放入 scrollViewDidScroll 中:

func scrollViewDidEndDragging(_ scrollView: UIScrollView,
                              willDecelerate decelerate: Bool) {
    if !decelerate {
        checkHasScrolledToBottom()
    }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    checkHasScrolledToBottom()
}

func checkHasScrolledToBottom() {
    let bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height
    if bottomEdge >= scrollView.contentSize.height {
        // we are at the end
    }
}

Actually, rather than just putting @bensnider's code in scrollViewDidScroll, this code (written in Swift 3) would be better performance-wise:

func scrollViewDidEndDragging(_ scrollView: UIScrollView,
                              willDecelerate decelerate: Bool) {
    if !decelerate {
        checkHasScrolledToBottom()
    }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    checkHasScrolledToBottom()
}

func checkHasScrolledToBottom() {
    let bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height
    if bottomEdge >= scrollView.contentSize.height {
        // we are at the end
    }
}
软糯酥胸 2024-11-20 08:43:20

它在 Swift 3 中工作:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y == (scrollView.contentSize.height - scrollView.frame.size.height) {
        loadMore()
    }
}

It work in Swift 3:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y == (scrollView.contentSize.height - scrollView.frame.size.height) {
        loadMore()
    }
}
演出会有结束 2024-11-20 08:43:20

使用诸如 indexPathsForVisibleRows 之类的内容查看 UIView 中当前显示的项目,如果您的模型包含的项目多于显示的项目,请在底部放置一个箭头。

See what items are currently displayed in the UIView, using something like indexPathsForVisibleRows and if your model has more items than displayed, put an arrow at the bottom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文