且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-17 16:05:04

如果您使用 UIScrollView 那么您可以从 scrollViewDidScroll:一>功能.您需要保存它拥有的最后一个位置(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.

希望对你有帮助.