滚动时将功能分配给UIScrollView(Swift)

问题描述 投票:0回答:1

我有一个UIViewController与我在UIScrollView添加的Interface Builder。我想拥有scrollView函数,以便我可以检测它是否已向上或向下滚动。我的滚动视图附有一个名为“mainScroll”的IBOutlet。这是我的代码:

var lastContentOffset: CGFloat = 0
@IBOutlet weak var mainScroll: UIScrollView!

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.lastContentOffset = mainScroll.contentOffset.y
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (self.lastContentOffset < mainScroll.contentOffset.y) {
        // did move up
        print("Did Move Up")
    } else if (self.lastContentOffset > mainScroll.contentOffset.y) {
        // did move down
        print("Did Move Down")
    } else {
        // didn't move
    }
}

我已经在课堂上添加了UIScrollViewDelegate。我有一种感觉,我没有正确使用scrollView委托回调。如何将这些功能分配给我的“mainScroll”?

ios swift uiscrollview interface-builder uiscrollviewdelegate
1个回答
1
投票

也许你需要在viewDidLoad这个

mainScroll.delegate = self
© www.soinside.com 2019 - 2024. All rights reserved.