我有一个带图像的UIScrollView,当用户滚动到scrollview的末尾时,我想更新内容。这是我的代码,用于检测何时到达滚动视图的底部:
以下代码在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但是它不起作用并且没有调用该函数
答案 0 :(得分:22)
如果你想在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
}}
答案 1 :(得分:4)
卡洛斯的回答更好。
对于Swift 4.x,您必须更改方法名称:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
//bottom reached
}
}
答案 2 :(得分:3)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
if (bottomEdge >= scrollView.contentSize.height)
{
// we are at the end
}
}
答案 3 :(得分:2)
你有没有想过添加一个布尔值。第一次调用方法时可能会更新它,也可能在用户向上滚动时更新它。
答案 4 :(得分:0)
实施scrollViewDidScroll:
并在其中检查contentOffset
以达到目的
答案 5 :(得分:0)
有时你必须在条件中使用+1,因为contentSize.height会给你一些小数,所以如果你使用它,你就可以避免它......
override func scrollViewDidScroll(scrollView: UIScrollView) {
if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
//bottom reached
}
}
答案 6 :(得分:0)
对于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
}
}
答案 7 :(得分:0)
为此我使用了混合方法。让我解释一下:
虽然您的微积分是正确的,但您听的委托是一种矫枉过正,因为 scrollViewDidScroll
被多次调用,这可能会导致性能问题。您应该(像我一样)使用 scrollViewDidEndDragging
和 scrollViewDidEndDecelerating
,它们在各自事件结束时仅被调用一次。
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
}
}