本文介绍了如何在Swift中获取UIScrollView垂直方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I get the scroll/swipe direction for up/down in a VC?

我想在我的VC中添加一个UIScrollView或其他东西,如果用户向上或向下滑动/滚动,然后隐藏/显示 UIView (取决于它是向上/向下手势)。

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 ,那么您可以从功能。您需要保存最后一个位置( 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
}

还有其他方法,当然这是一个给他们。

There are other ways of do it of course this is one to them.

我希望这能帮助你。

这篇关于如何在Swift中获取UIScrollView垂直方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:52