为什么滚动视图为零



我有以下代码:

extension SegmentedControlViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
if abs(scrollView.contentOffset.x - 10) < 1 {
print("works")
}
}
}
@IBOutlet weak var scrollView: UIScrollView!

然后我调用视图中的函数DidLoad,如下所示:

scrollViewDidScroll(scrollView)

尽管我总是收到错误消息

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

我做错了什么,如何使滚动视图不为零。

viewDidLoad()中指定滚动视图的.delegate并开始滚动:

class SegmentedControlViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// assign the delegate      
scrollView.delegate = self

}
}
extension SegmentedControlViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
if abs(scrollView.contentOffset.x - 10) < 1 {
print("works")
}
}
}

编辑

如果您想检查滚动视图内容是否已向右拖动超过10点,请使用以下选项:

extension SegmentedControlViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
if scrollView.contentOffset.x < 10 {
print("scroll content has been dragged to the right more than 10 points")
}
}
}

相关内容

  • 没有找到相关文章

最新更新