Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringWithDamping for CALayer animations?

After playing around a lot with the UIView dynamic animations introduced in iOS 7, most notably:

[UIView animateWithDuration: delay: usingSpringWithDamping: initialSpringVelocity: options: animations: completion:];

I was wondering if there is an equivalent to 'SpringWithDamping/Velocity' method that can be accessed directly when creating a CALayer animation? I.e. either through CATransaction, CABasicAnimation or otherwise...

Thanks

like image 318
Sarreph Avatar asked Mar 11 '14 17:03

Sarreph


3 Answers

in iOS9 Apple finally made the CASpringAnimation class public.

You can use it like that:

let spring = CASpringAnimation(keyPath: "position.x")
spring.damping = 5
spring.fromValue = myLayer.position.x
spring.toValue = myLayer.position.x + 100.0
spring.duration = spring.settlingDuration
myLayer.addAnimation(spring, forKey: nil)

Notice that you cannot set the animation duration - you need to ask the CASpringAnimation class for the settlingDuration (e.g. "How much time is going to take for the spring system to settle down") and then set it as the duration of your animation.

Check the header files for CASpringAnimation - it exposes a number of spring system variables you can adjust - stiffness, mass, etc.

like image 149
Marin Todorov Avatar answered Nov 18 '22 09:11

Marin Todorov


There is (and have been for a while) a private class called CASpringAnimation that I'm pretty sure is being used behind it all (but I haven't verified it). Unfortunately, it is still private.

like image 6
David Rönnqvist Avatar answered Nov 18 '22 11:11

David Rönnqvist


As David said, CASpringAnimation is private (for now?), but I recently came across RBBSpringAnimation from the RBBAnimation project.

I can definitely recommend this, it was very easy to drop in as a replacement for my existing CABasicAnimation.

like image 4
dmur Avatar answered Nov 18 '22 11:11

dmur