1
votes

I want to make a view/header collaspe/expand animation, but I also want to be able to pause or stop from expanding/collapsing while the user takes his finger off the screen.

I have a current implementation for this, but the animation is not very smooth.

I will also add some images to explain better what I want to do.

So the first image is how it should look when expanded.

The second image is how it should look if the user decides to take take his finger off the screen while he would scroll slowly so the animation/the view should stop in a state similar to that.

The third image is how it should look when it is collapsed.

If someone has any recommendation how I could achieve this I would be very grateful.

expanded

mid expanded

collapsed

1
show the code that you already have - Milan Nosáľ
The code that I already have needs to be completly replaced. It was made by another developer and it was generated by some tool. I want to do it from scratch - razvan

1 Answers

2
votes

If you are targeting iOS 10+, use UIViewPropertyAnimator which encapsulates an animation and allows scrubbing and controlling the animation:

Start, pause, resume, and stop animations; see the methods of the UIViewAnimating protocol.

Add animation blocks after the original animations start using the addAnimations(_:) and addAnimations(_:delayFactor:) methods.

Scrub through a paused animation by modifying the fractionComplete property.

Change the animation’s direction using the isReversed property.

Modify the timing and duration of a partially complete animation by pausing the animation and using the continueAnimation(withTimingParameters:durationFactor:) method to finish it.

You just need to wire up touches to UIViewPropertyAnimator object encapsulating the animation.

There are many good tutorials on how to start with it, e.g. this one.