Although I have used UIScrollView many, many times, I know that it has some sliding proxy callback methods, and I know that it has some sliding state-related properties, but I am not sure about the specific state of these methods at each point in time, and I am confused by the official documentation. Simply call all the proxy methods and properties this time and record the results.
For such description involving state changes, words seemed to be too weak, so I tried to record them in the form of charts in order to be more intuitive.
Scenario 1: Drag to accelerate and then release and slide freely
If you slide to the bottom and the ScrollView bounces back, then the isDraging property of the last scrollViewDidScroll is false
Situation 2: Drag slowly and release
conclusion
-
IsDraging: Indicates whether the scrollView is sliding freely or whether the finger is currently touching the screen
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. Copy the code
-
IsTracking: indicates whether the current finger contacts the screen
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. Copy the code
-
Isactivity, which is simpler, isDecelerating, which is true only when it’s moving inertia
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. Copy the code
Code
The complete code is attached for readers to test and verify independently
//
// TableViewVC.swift
// HLTest
//
// Created by Hanley Lee on 2021/5/19.
Copyright © 2021 Hanley Lee. All rights reserved.
//
import UIKit
class TableViewVC: UITableViewController {
var startTime: CFTimeInterval = .init(a)var des: String {
return "isDragging: \(tableView.isDragging), isTracking: \(tableView.isTracking), isDecelerating: \(tableView.isDecelerating), interval: \(CACurrentMediaTime() - startTime)"
}
override func viewDidLoad(a) {
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) as? TableViewCell) ?? .init(frame: .zero)
cell.setContent(with: indexPath.row.description)
return cell
}
}
Copy the code