UIViewPropertyAnimator:: User can drag-scroll past ( indefinitely) image start/end whilst animation is in progress

I have implemented an animation using UIViewPropertyAnimator to scroll a horizontal stack view.


            animator = UIViewPropertyAnimator(duration: ct, curve: .easeInOut) {
                self.scrollView.contentOffset = CGPoint(x: self.horizStackView.bounds.width - screenWidth, y: 0)
            }
          
          animator?.isInterruptible = true
          animator?.isUserInteractionEnabled = true


Whilst the animation is in progress the user can use drag gesture to scroll the image left or right. My issue is that the animator allows the image to be scrolled past the image ENDS (start or end) indefinately. However when the animation is paused or finished the user cannot scroll past the image ends. This is the desired behaviour. If I disable isUserInteractionEnabled then user cannot scroll which is FINE but it also disables the TAP gesture. I really need the TAP gesture to work as I reveal Play/Pause button.

Can help? How can I stop user scrolling past image ends indefinitely whilst animation is in progress.

Answered by Daxa in 420928022

I have now resolved this problem by setting scrollView.isScrollEnabled = falsewhilst animating and re-enable when the animation is completed. This stops the user do the pan or swipe gesture whilest animating. This serves my purpose.

What I read from documentation:

A UIViewPropertyAnimator object lets you animate changes to views and dynamically modify your animations before they finish. With a property animator, you can run your animations from start to finish normally or you can turn them into interactive animations and control the timing yourself. The animator operates on animatable properties of views, such as the frame, center, alpha, and transform properties, creating the needed animations from the blocks you provide.


Not sure contentOffset can be animatable property ?

I was not able to find sure information about it.

See discussion here which seems to point at this:

h ttps://forums.raywenderlich.com/t/uiviewpropertyanimator-inconsistency/40021


Don't think this can apply to your case, but someone found a hack on a problem with Animator by setting

.completionSpeed = 0.999

https://stackoverflow.com/questions/45401377/uipercentdriveninteractivetransition-cancelling-issue

Thanks Claude, I do not have an issue with the animation it works fine. It was the fact that a user can scroll the image Left/Right whilst animation is playing and he/she is able to scroll past the image ends.

Accepted Answer

I have now resolved this problem by setting scrollView.isScrollEnabled = falsewhilst animating and re-enable when the animation is completed. This stops the user do the pan or swipe gesture whilest animating. This serves my purpose.

Thanks for the feedback.


Did you consider overriding

func touchesShouldCancel(in view: UIView) -> Bool
UIViewPropertyAnimator:: User can drag-scroll past ( indefinitely) image start/end whilst animation is in progress
 
 
Q