如何在 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/