ioscontentoffset

Tracking a specific value with contentOffSet


I want to change the frame of a view while the user is scrolling. I'm checking the contentOffSet of the scroll view in scrollViewDidScroll, and I want to know when the contentOffSet reach a specific value. But I figure out if the user is scrolling faster, that value is skipped.

Do you know a method to track the contentOffSet in a precise way?


Solution

  • There's no way to track the scrolling with finer precision.

    The best thing to do, would be to check that the contentOffset is greater than or less than what you're looking for (depending on the circumstances).

    You'd also need a way of checking if the cutoff point had already been reached (so that you didn't execute multiple times).

    private var triggeredAnimation = false
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !triggeredAnimation, scrollView.contentOffset.y > 100 {
            triggeredAnimation = true
            // Cut off point reached, do whatever you want now
        }
    }
    

    Of course, if whatever action you're performing is reversible, you'd need to reset when going the other way.