如何在Swift中获取UIScrollView垂直方向?

use*_*667 26 cocoa-touch uiscrollview uiviewcontroller ios swift

如何在VC中获得向上/向下的滚动/滑动方向?

我想在我的VC中添加一个UIScrollView或其他东西,可以查看用户是向上还是向下滑动/向上滚动,然后隐藏/显示UIView取决于它是否为向上/向下手势.

Vic*_*ler 77

如果您使用a,UIScrollView那么您可以从该scrollViewDidScroll:功能中受益.您需要保存它的最后位置(contentOffset),并按以下方式更新它:

// variable to save the last position visited, default to zero
private var lastContentOffset: CGFloat = 0

func scrollViewDidScroll(scrollView: UIScrollView!) {
    if (self.lastContentOffset > scrollView.contentOffset.y) {
        // move up
    }
    else if (self.lastContentOffset < scrollView.contentOffset.y) { 
       // move down
    }

    // update the new position acquired
    self.lastContentOffset = scrollView.contentOffset.y
}
Run Code Online (Sandbox Code Playgroud)

还有其他方法,当然这是他们之一.

我希望这对你有帮助.

  • 谢谢,有点工作。但是由于滚动视图具有弹跳效果,因此如果我一直滚动到顶部或执行“拉动刷新”操作,它会前后多次触发函数“ didScrollUp”和“ didScrollDown”,这会导致视图切换之间隐藏/显示几次。所以maby我要向滚动视图/表格视图添加滑动手势? (3认同)

小智 43

Victor的答案很棒,但它相当昂贵,因为你总是比较和存储价值观.如果您的目标是立即识别滚动方向而无需进行昂贵的计算,那么请使用Swift尝试:

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
    if translation.y > 0 {
        // swipes from top to bottom of screen -> down
    } else {
        // swipes from bottom to top of screen -> up
    }
}
Run Code Online (Sandbox Code Playgroud)

你去吧 同样,如果你需要不断跟踪,请使用Victors答案,否则我更喜欢这个解决方案.


gbo*_*tha 11

我用Victor的答案略有改进.滚动滚动结束或开始滚动,然后获得反弹效果.我通过计算scrollView.contentSize.height - scrollView.frame.height然后将scrollView.contentOffset.y范围限制为大于0或小于添加约束,scrollView.contentSize.height - scrollView.frame.height在弹回时不进行任何更改.

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if lastContentOffset > scrollView.contentOffset.y && lastContentOffset < scrollView.contentSize.height - scrollView.frame.height {
        // move up
    } else if lastContentOffset < scrollView.contentOffset.y && scrollView.contentOffset.y > 0 {
        // move down
    }

    // update the new position acquired
    lastContentOffset = scrollView.contentOffset.y
}
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案 (3认同)

Pet*_*kos 7

我已经尝试了该线程中的每一个响应,但没有一个可以为启用了反弹的 tableView提供适当的解决方案。所以我只使用了部分解决方案以及一些历史悠久的经典布尔标志解决方案。

1) 所以,首先你可以为 scrollDirection 使用枚举:

enum ScrollDirection {
    case up, down
}
Run Code Online (Sandbox Code Playgroud)

2) 设置 3 个新的私有变量来帮助我们存储 lastOffset、scrollDirection 和一个标志来启用/禁用滚动方向计算(帮助我们忽略 tableView 的反弹效果),稍后您将使用它:

private var shouldCalculateScrollDirection = false
private var lastContentOffset: CGFloat = 0
private var scrollDirection: ScrollDirection = .up
Run Code Online (Sandbox Code Playgroud)

3) 在scrollViewDidScroll添加以下内容:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    // The current offset
    let offset = scrollView.contentOffset.y

    // Determine the scolling direction
    if lastContentOffset > offset && shouldCalculateScrollDirection {
        scrollDirection = .down
    }
    else if lastContentOffset < offset && shouldCalculateScrollDirection {
        scrollDirection = .up
    }

    // This needs to be in the last line
    lastContentOffset = offset
}
Run Code Online (Sandbox Code Playgroud)

4) 如果你还没有实现scrollViewDidEndDragging实现它并在其中添加以下代码行:

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    guard !decelerate else { return }
    shouldCalculateScrollDirection = false
}
Run Code Online (Sandbox Code Playgroud)

5) 如果你还没有实现scrollViewWillBeginDecelerating实现它并在里面添加这行代码:

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    shouldCalculateScrollDirection = false
}
Run Code Online (Sandbox Code Playgroud)

6) 最后,如果你还没有实现scrollViewWillBeginDragging实现它并在里面添加这行代码:

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {    
    shouldCalculateScrollDirection = true
}
Run Code Online (Sandbox Code Playgroud)

如果您遵循上述所有步骤,您就可以开始了!

你可以去任何你想使用方向的地方,然后简单地写:

switch scrollDirection {
case .up:
    // Do something for scollDirection up
case .down:
    // Do something for scollDirection down
}
Run Code Online (Sandbox Code Playgroud)


Har*_*kar 6

迅捷4

public func scrollViewDidScroll(_ scrollView: UIScrollView) {

        if(scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0) {
            print("up")
        }
        else {
            print("down")
        }
    }
Run Code Online (Sandbox Code Playgroud)