top of page

67. Article: How to know if ScrollView, List is scrolling

Writer's picture: Hui WangHui Wang

It is very important in some scenarios if you want to know if a scrollable control ( ScrollView, List ) is in a scrolling state.


For example, in SwipeCell, we need to close the opened side menu automatically when the scrollable component starts to scroll.


Unfortunately, SwiftUI does not provide an API for this, and we need to implement it ourselves.


Several solutions could achieve the function of "observing the current scroll state." However, each one has its advantages and limitations.


1, Introspect: By accessing the UIKit controls behind the Delegate.

Pros: Accurate, timely, and low system burden.

Cons: Bad downward/backward compatibility.


2, Runloop: Create a TimerPublisher bound to different Runloop modes to judge the scrolling state.

Pros: Simple, accurate, and timely.

Cons: It only supports iOS, and there can only be one scrollable control on the screen.


3, PreferenceKey: Subviews can pass information to their ancestor views through preference.

Pros: It supports multiple platforms, with downward/backward and upward/forward compatibility.

Cons: You must add modifiers to subviews of scrollable controls, and the accuracy is not as good as the first two solutions.


Here I recommend an open source library IsScrolling, which is designed for the above three solutions, and you can refer to it.



References:


IsScrolling


 

Follow me on:

Comments


Never Miss a Post. Subscribe Now!

I am an iOS developer, and I will share some knowledge related to iOS development from time to time.

Thanks for submitting!

© 2022 by (Foks) Hui Wang

bottom of page