从未调用事件滚动 (scrollViewDidScroll)

编程入门 行业动态 更新时间:2024-10-05 19:16:24
本文介绍了从未调用事件滚动 (scrollViewDidScroll) - Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想检测用户何时滚动,我使用了 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 01:59:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   scrollViewDidScroll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!