UIScrollView 的相关滑动代理回调方法及属性在滑动时的变化

news/2024/7/20 20:13:47 标签: ios, viewer

如题, 虽然用了很多很多次 UIScrollView, 知道它有一些滑动代理回调方法, 知道它有一些滑动状态相关的属性, 但对这些方法在每一个时间点的具体状态总是不太确定, 看官方说明文档也是一头雾水. 索性这次将所有的代理方法及属性调用一遍, 然后记录下结果.

对于这种涉及到状态变化的描述, 文字似乎有些苍白无力, 于是我尝试用图表的方式来进行记录, 以期更加直观.

情景一: 拖拽加速然后松开自由滑动

himg
himg

如果滑动到底部, 且 ScrollViewbounces 回弹效果的话, 那么最后一次的 scrollViewDidScrollisDraging 属性为 false

情景二: 缓慢拖拽然后松开

himg
himg

总结

  • isDraging: 表示 scrollView 是否在自由滑动或者当前手指在接触屏幕

    A Boolean value that indicates whether the user has begun scrolling the content.
    

The value held by this property might require some time or distance of scrolling before it is set to true.

  • isTracking: 表示当前手指是否与屏幕接触

    Returns whether the user has touched the content to initiate scrolling.
    
  • The value of this property is true if the user has touched the content view but might not have yet have started dragging it.

  • isDecelerating: 这个简单的多, 就是表示是否正在减速, 也就是说只有当惯性滑动的时候才会为 true

    Returns whether the content is moving in the scroll view after the user lifted their finger.
    
  • The returned value is true if user isn’t dragging the content but scrolling is still occurring.

    Code

    为了便于读者独立测试验证, 这里附上完整代码

    //
    //  TableViewVC.swift
    //  HLTest
    //
    //  Created by Hanley Lee on 2021/5/19.
    //  Copyright © 2021 Hanley Lee. All rights reserved.
    //

    import UIKit

    class TableViewVCUITableViewController {

        var startTime: CFTimeInterval = .init()
        var des: String {
            return "isDragging: \(tableView.isDragging), isTracking: \(tableView.isTracking), isDecelerating: \(tableView.isDecelerating), interval: \(CACurrentMediaTime() - startTime)"
        }

        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.rowHeight = 50
            tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
            startTime = CACurrentMediaTime()
        }


        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
        }


        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            let timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { _ in
                print(self.des)
            }

            RunLoop.current.add(timer, forMode: .common)
        }

    }

    // MARK: - Scroll Delegate

    extension TableViewVC {
        override func scrollViewDidScroll(_ scrollView: UIScrollView) {
            print("scrollViewDidScroll: \(des)")
        }

        override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
            print("scrollViewWillBeginDragging: \(des)")
        }

        override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
            print("scrollViewWillEndDragging: \(des)")
        }

        override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
            print("scrollViewDidEndDragging: \(des)")
        }

        override func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
            print("scrollViewWillBeginDecelerating: \(des)")
        }

        override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
            print("scrollViewDidEndDecelerating: \(des)")
        }
    }

    // MARK: - TableView Delegate & Data Source

    extension TableViewVC {

        override func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }

        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 100
        }

        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = (tableView.dequeueReusableCell(withIdentifier: "TableViewCell"for: indexPath) asTableViewCell) ?? .init(frame: .zero)
            cell.setContent(with: indexPath.row.description)
            return cell
        }
    }

http://www.niftyadmin.cn/n/824625.html

相关文章

Vim 之代码跟踪

这一篇聊聊我们如何通过 vim 浏览代码 代码浏览最重要的就是跟踪代码, 跟踪定义, 跟踪声明, 跟踪调用, 跟踪引用... himgvim 的跟踪通常可以通过两种方式实现: tags: 通过 ctags 工具生成 tags 文件cscope 数据库: 通过 cscope 或 gtags-cscope 生成 cscope 数据库两种方法各有…

配置hexo博客

title: 通过Hexo搭建个人博客 date: 2019-07-28 22:32:06 categories: Project 本地搭建hexo 准备工作&#xff1a; sudo  apt  install  npm sudo  apt  install  nodejs sudo  npm  install  hexo-cli -g hexo  --version  # 查看是否安装成功 新建博客项目…

Shell 在 MacOS 及 Linux 中的文件读取顺序

Shell 在 MacOS 及 Linux 中的文件读取顺序 himgMacOS 与 Linux 中 zsh 的加载顺序 Interactive loginInteractive non-loginScript/etc/zshenvAAA~/.zshenvBBB/etc/zprofileC~/.zprofileD/etc/zshrcEC~/.zshrcFD/etc/zloginG~/.zloginH~/.zlogoutI/etc/zlogoutJ 如文章首图与上…

jupyter notebook添加anaconda环境

pip install ipykernel python -m ipykernel install --user --nametensorflow_gpuenv#那个环境的名字

Variable Expansion in Applescript

AppleScript 是 Apple 平台 用来操控系统及 app 的一种脚本语言, 简单使用时非常便利, 但是在一些灵活场景下便难以胜任, 这篇谈谈我遇到的 variable expansion 问题 himg事件背景: EuDic 提供了 AppleScript 脚本控制功能, 我想要写一个 AppleScript 脚本来快速查找单词, 但是…

Netstat命令使用方法

Netstat 简介 Netstat 是一款命令行工具&#xff0c;可用于列出系统上所有的网络套接字连接情况&#xff0c;包括 tcp, udp 以及 unix 套接字&#xff0c;另外它还能列出处于监听状态&#xff08;即等待接入请求&#xff09;的套接字。如果你想确认系统上的 Web 服务有没有起来…

如何在 Vim 中使用外部命令的输出

在 vim 中我们可以用添加前缀 ! 的方式执行外部命令, 例如 !ls, 其结果将被在底部输出 himg那么我们如果想使用外部命令的结果, 该怎么做呢? 使用 :read :read 可以读取命令执行结果到当前 buffer 中, 如果我们想插入外部命令的结果, 那么使用 :read !ls 即可 himg使用 system…

markdown颜色索引及表格背景颜色填充

颜色名十六进制颜色值颜色AliceBlue#F0F8FFrgb(240, 248, 255)AntiqueWhite#FAEBD7rgb(250, 235, 215)Aqua#00FFFFrgb(0, 255, 255)Aquamarine#7FFFD4rgb(127, 255, 212)Azure#F0FFFFrgb(240, 255, 255)Beige#F5F5DCrgb(245, 245, 220)Bisque#FFE4C4rgb(255, 228, 196)Black#00…