When working on a large project spanning several developers, I often find that each developer has their own personal favorite animation timing. On our latest project, I implemented a simple UIView extension that lets us standardize these durations across the app using named enum values rather than inline floats or global constants. Here’s the extension:

enum AnimationDuration: NSTimeInterval {
    case Standard = 0.25
    case Slow = 0.75
    case VerySlow = 1.5
    case Debug = 10.0
}

extension UIView {
    static func animateWithDuration(duration: AnimationDuration, animations: (Void) -> Void) {
        self.animateWithDuration(duration.rawValue, animations: animations)
    }
    
    static func animateWithDuration(duration: AnimationDuration,
                                    delay: NSTimeInterval = 0,
                                    options: UIViewAnimationOptions = [.CurveEaseInOut, .TransitionNone],
                                    animations: (Void) -> Void,
                                    completion: ((Bool) -> Void)? = nil) {
        self.animateWithDuration(duration.rawValue, delay: delay, options: options, animations: animations, completion: completion)
    }
}

extension CAAnimation {
    var animationDuration: AnimationDuration? {
        get {
            return AnimationDuration(rawValue: self.duration)
        }
        set {
            self.duration = animationDuration?.rawValue ?? 0
        }
    }
}

And here’s an example of how to use it in practice:

UIView.animateWithDuration(.Slow) {
    //animate properties here
}

As always, feel free to reach out to us with questions or comments @untoldhq on Twitter.