Event scrolling (scrollViewDidScroll) never called - Swift 3

杀马特。学长 韩版系。学妹 提交于 2019-12-10 23:37:28

问题


I would like to detect when the user scroll, I used a UIScrollView, I implemented the UIScrollViewDelegate in my ViewController and tried scrollViewDidScroll, scrollViewDidEndScrollingAnimation and all the others but these events are never called.

import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {     

    @IBOutlet weak var scrollView: UIScrollView!    

    override func viewDidLoad() {        
        super.viewDidLoad()    

        let xOrigin = self.view.frame.width

        let view1 = View1(frame: CGRect(x: 0, y: 0, width:self.view.frame.width, height: self.view.frame.height))
        let view2 = View2(frame: CGRect(x: xOrigin, y: 0, width: self.view.frame.width, height: self.view.frame.height))
        let view3 = View3(frame: CGRect(x: xOrigin * 2, y: 0, width: self.view.frame.width, height: self.view.frame.height))

        scrollView.addSubview(view1)
        scrollView.addSubview(view2)
        scrollView.addSubview(view3)

        self.scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)

        // hide the scrol bar.
        scrollView?.showsHorizontalScrollIndicator = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("end scroll")
    }


    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        print("end scroll")
    }
}

回答1:


You just have to add the delegate to your viewDidLoad() func:

override func viewDidLoad() {        
    super.viewDidLoad()  
    //add this line
    self.scrollView.delegate = self

 }


来源:https://stackoverflow.com/questions/41174803/event-scrolling-scrollviewdidscroll-never-called-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!