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

标签 ios swift cocoa-touch uiviewcontroller uiscrollview

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

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

最佳答案

如果您使用 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
     print(lastContentOffset)
 }

当然还有其他方法可以做到这一点。

希望对你有帮助

关于ios - 如何在 Swift 中获取 UIScrollView 垂直方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31857333/

相关文章:

IOS 双击触发 2 segues

ios - 如何设置启动图像以支持 iOS 6 到 iOS 9?

ios - 在 UIView 中画线

ios - 触摸对象GLKIT或OpenGL有什么功能?

objective-c - 如何从 lldb 中的 objc 访问快速便利变量?

ios - Swift 多文本字段参数

ios - 这个控制台警告对我的约束意味着什么?

swift - 连续包含不同项目的 Collectionview

objective-c - UITextField 委托(delegate)

ios - 在 iOS 7 中显示键盘时出现问题 'Resizing' UITextView