Search code examples
iosswiftuiscrollviewcontentoffset

Track UIScrollView contentOffset all the time


I'm using a timer and print the contentOffset.y of a UIScrollView to track it all the time.

But the problems is UIScrollView updates its contentOffset value only after it is decelerated.

I want to know the contentOffset.y value in every millisecond of timer and how can I do that? If I cannot use contentOffset of the UIScrollView at all, is there any other way to track similar value to contentOffset.y of a scrollView?

I'm using swift. Thank you.


Solution

  • Instead of tracking the changes by polling with a timer, you can observe any changes to the value. UIScrollView has a delegate property that gets called for scroll events (see UIScrollViewDelegate), and contentOffset is KVO compliant.

    Or you could subclass UIScrollView and try overriding the setters (either for contentOffset or bounds where origin is the the offset, I'm not sure which one, if either, the class calls internally - using the delegate is the preferred solution).