Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
/ ModularAnimation Public archive

A small framework that seperates the playback and the animation description from the (UIView-)animation itself

License

Notifications You must be signed in to change notification settings

lotum/ModularAnimation

Repository files navigation

ModularAnimation

A small framework that seperates the playback and the animation description from the (UIView-)animation itself.

ModularAnimation gets rid of the drawbacks of UIView.animate():

  • Your animation code will be easy to understand
  • You will be able to reuse your animation description on different views

tl;dr

You can write this

let description = AnimationAction.serial(
    .scaleEqually(scale: 2),
    .delay(duration: 0.4),
    .springTransformBack(duration: 0.25),
    .parallel(
        .fadeOut(duration: 0.4),
        .scale(x: 5, y: 0.1, duration: 0.4)
    )
)

// use same description on different views and playback when you like to do it
let object1 = UIView()
let object2 = UIView()

object1.animation(description).play()
object2.animation(description).play()

instead of this

let object = UIView()
    
UIView.animate(
    withDuration: 0,
    delay: 0,
    options: [],
    animations: {
        object.transform.scaledBy(x: 2, y: 2)
},
    completion: { _ in
        UIView.animate(
            withDuration: 0.25,
            delay: 0.25,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 4,
            options: [],
            animations: {
                object.transform = .identity
        },
            completion: { _ in
               UIView.animate(
                    withDuration: 0.4,
                    delay: 0,
                    options: [],
                    animations: {
                        object.alpha = 0
                        object.transform.scaledBy(x: 5, y: 0.1)
                },
                    completion: nil
                )
        }
        )
    }
)

Usage

Seperate the playback with ModularAnimation

let object = UIView()

let scaleUp = BasicAnimationModule(duration: 0) {
    object.transform.scaledBy(x: 2, y: 2)
}
let scaleBack = SpringAnimationModule(duration: 0.25, delay: 0.25, dampingRatio: 0.8, velocity: 4) {
    object.transform = .identity
}
let fadeOut = BasicAnimationModule(duration: 0.4) {
    object.alpha = 0
}
let squeeze = BasicAnimationModule(duration: 0.4) {
    object.transform.scaledBy(x: 5, y: 0.1)
}

// you can do this (similar to before)
scaleUp.play() { _ in
    scaleBack.play() { _ in
        fadeOut.play()
        squeeze.play()
    }
}

// or this to get rid of the completion blocks
let fadeOutAndSqueeze = ParallelAnimation(fadeOut, squeeze)
let serial = SerialAnimation(scaleUp, scaleBack, fadeOutAndSqueeze)
serial.play()

Seperate the animation description

let description = AnimationAction.serial(
    .scaleEqually(scale: 2),
    .delay(duration: 0.4),
    .springTransformBack(duration: 0.25),
    .parallel(
        .fadeOut(duration: 0.4),
        .scale(x: 5, y: 0.1, duration: 0.4)
    )
)

// use same description on different views
let object1 = UIView()
let object2 = UIView()

object1.animation(description).play()
object2.animation(description).play()

All in all you can write this compact and easy to grasp animation code for the view:

let object = UIView()

object
    .serialAnimation(
        .scaleEqually(scale: 2),
        .delay(duration: 0.4),
        .springTransformBack(duration: 0.25),
        .parallel(
            .fadeOut(duration: 0.4),
            .scale(x: 5, y: 0.1, duration: 0.4)
        )
    ).play()

Requirements

Swift 3.1

Installation

ModularAnimation is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "ModularAnimation"

License

ModularAnimation is available under the Apache license. See the LICENSE file for more info.

About

A small framework that seperates the playback and the animation description from the (UIView-)animation itself

Resources

License

Stars

Watchers

Forks

Packages

No packages published