从未调用事件滚动 (scrollViewDidScroll) - Swift 3 [英] Event scrolling (scrollViewDidScroll) never called - Swift 3

查看:28
本文介绍了从未调用事件滚动 (scrollViewDidScroll) - Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测用户何时滚动,我使用了 UIScrollView,我在 ViewController 中实现了 UIScrollViewDelegate 并尝试了 scrollViewDidScroll、scrollViewDidEndScrollingAnimation 和所有其他事件,但这些事件从未被调用.

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")
    }
}

推荐答案

您只需将委托添加到您的 viewDidLoad() 函数:

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

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

 }

这篇关于从未调用事件滚动 (scrollViewDidScroll) - Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆