我有一个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”?
也许你需要在viewDidLoad
这个
mainScroll.delegate = self