0
0
iOS Swiftmobile~5 mins

Spring animations in iOS Swift

Choose your learning style9 modes available
Introduction

Spring animations make movements in your app look smooth and natural, like things bouncing or settling gently.

When you want a button to bounce when tapped.
To animate a view sliding in with a spring effect.
When showing or hiding elements with a lively motion.
To make dragging or moving objects feel more real.
When you want to add playful feedback to user actions.
Syntax
iOS Swift
UIView.animate(withDuration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIView.AnimationOptions, animations: @escaping () -> Void, completion: ((Bool) -> Void)?)

usingSpringWithDamping controls how bouncy the animation is. Values close to 1 mean less bounce.

initialSpringVelocity sets the speed at which the animation starts.

Examples
This moves the view down by 100 points with a spring bounce effect.
iOS Swift
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [], animations: {
    view.center.y += 100
}, completion: nil)
This fades out the view smoothly with a spring effect and then hides it.
iOS Swift
UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.0, options: [], animations: {
    view.alpha = 0
}, completion: { finished in
    view.isHidden = true
})
Sample App

This app shows a blue square. When you tap it, the square grows bigger with a spring bounce, then returns to normal size with another spring animation.

iOS Swift
import UIKit

class ViewController: UIViewController {
    let box = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        box.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        box.backgroundColor = .systemBlue
        view.addSubview(box)

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(animateBox))
        box.addGestureRecognizer(tapGesture)
        box.isUserInteractionEnabled = true
    }

    @objc func animateBox() {
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.8, options: [], animations: {
            self.box.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
        }, completion: { _ in
            UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: [], animations: {
                self.box.transform = .identity
            }, completion: nil)
        })
    }
}
OutputSuccess
Important Notes

Spring animations feel more natural than simple linear animations.

Adjust usingSpringWithDamping and initialSpringVelocity to get the bounce you want.

Remember to keep animations short to keep the app feeling responsive.

Summary

Spring animations add bounce and life to your app's movements.

Use UIView.animate with spring parameters for smooth effects.

Experiment with damping and velocity to find the right feel.