問題描述
如何在 VC 中獲取向上/向下的滾動(dòng)/滑動(dòng)方向?
How can I get the scroll/swipe direction for up/down in a VC?
我想在我的 VC 中添加一個(gè) UIScrollView 或其他東西,可以查看用戶是否向上或向下滑動(dòng)/滾動(dòng),然后隱藏/顯示 UIView
取決于它是否是向上/向下手勢(shì).
I want to add a UIScrollView or something else in my VC that can see if the user swipes/scrolls up or down and then hide/show a UIView
depending if it was an up/down gesture.
推薦答案
如果你使用 UIScrollView
那么你可以從 scrollViewDidScroll:
功能.您需要保存它的最后一個(gè)位置(contentOffset
)并按以下方式更新它:
If you use an UIScrollView
then you can take benefit from the scrollViewDidScroll:
function. You need to save the last position (the contentOffset
) it have and the update it like in the following way:
// 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
}
當(dāng)然還有其他方法可以做到這一點(diǎn).
There are other ways of do it of course this is one to them.
希望對(duì)你有所幫助.
這篇關(guān)于如何在 Swift 中獲取 UIScrollView 的垂直方向?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!