Search code examples
iosswiftuiscrollviewuikitcore-animation

Halve scroll speed for UIScrollView?


For testing (and for screen captures) I use layer.speed to slow down animations globally.

window.layer.speed = 0.5

Yet this only controls animations, I cannot control UIScrollView speeds. Is there any (either undocumented/private) way to halve the deceleration speed of scroll views?

I'm aware of UIScrollView.decelerationRate, yet it seems only work with the .normal and .fast values. I'm interested in slowing it down.


Solution

  • You can set any value to decelerationRate using raw value initializer.

    scrollView.decelerationRate = .init(rawValue: 0.998)
    

    This won't work for paging UIScrollView instances. For that, you need to set the pagingFriction property.

    scrollView.setValue(0.9422507685, forKey: "pagingFriction")
    

    For content offset animations (e.g. animation after refresh).

    scrollView.setValue(0.6, forKey: "contentOffsetAnimationDuration")
    

    Thanks to this awesome iOS-Headers repo.

    UPDATE: The best thing is the swizzling for tweaking deceleration rate from a bounce. I can't make it fit into this answer, see UIScrollView+Extensions.swift for details.