如何在 VC 中获得向上/向下滚动/滑动方向?
我想在我的 VC 中添加 UIScrollView 或其他内容,可以查看用户是否向上或向下滑动/滚动,然后隐藏/显示UIView
取决于它是否是向上/向下手势。
如果您使用UIScrollView
那么您就可以受益于scrollViewDidScroll: https://developer.apple.com/library/prerelease/ios//documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIScrollViewDelegate/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)
}
还有其他方法可以做到这一点,当然这是其中之一。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)