Animate

class Animate

Class for creating animations. Chains together animation ‘sentences’ using self-returning functions and completion handlers.

Animate(view, duration).setOptions(.curveLinear).rotate(to: 90)
let view = Animate(view, duration)
view.setSpring(damping, velocity).translate(by: [50, 0], then: {
    view.flip().shake(then: {
       view.reset()
    })
})

Author

Cadence Holmes 2018
  • Undocumented

    Declaration

    Swift

    typealias Completion = () -> ()
  • Undocumented

    Declaration

    Swift

    private var duration: Double
  • Undocumented

    Declaration

    Swift

    var originalDuration: Double
  • Undocumented

    Declaration

    Swift

    private var origin: Array<CGFloat>
  • Undocumented

    Declaration

    Swift

    init(_ view: UIView, _ duration: Double? = nil)
  • Set the options for all animations.

    See UIView.animate(options: UIViewAnimationOptions) for more information.

    Declaration

    Swift

    @discardableResult
    func setOptions(_ options: UIViewAnimationOptions) -> Animate
  • Set the spring damping and velocity for all animations.

    See UIView.animate(usingSpringWithDamping:) and UIView.animate(initialSpringVelocity:) for more information.

    Declaration

    Swift

    @discardableResult
    func setSpring(_ damping: CGFloat, _ velocity: CGFloat) -> Animate
  • Set the delay for all animations.

    Declaration

    Swift

    @discardableResult
    func setDelay(_ delay: Double) -> Animate
  • Set the duration for all animations.

    Declaration

    Swift

    @discardableResult
    func setDuration(_ duration: Double) -> Animate
  • Convenience function that sets animation durations to 0 (instant).

    Declaration

    Swift

    @discardableResult
    func now() -> Animate
  • Convenience function that sets animation durations to back to the last set duration.

    Declaration

    Swift

    @discardableResult
    func notnow() -> Animate
  • Cancel current animations on the view layer. The transform does not reset.

    Declaration

    Swift

    @discardableResult
    func cancel(then: Completion? = nil) -> Animate

    Parameters

    then

    Nullable completion handler, executed after the duration of the animation.

  • Chainable wait function.

    Declaration

    Swift

    @discardableResult
    func wait(asec: Double, then: Completion?) -> Animate

    Parameters

    asec

    Time in seconds to wait.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Tilt animation. Tilt in one direction, then tilt back to origin.

    Declaration

    Swift

    @discardableResult
    func tilt(degrees: Double, then: Completion? = nil) -> Animate

    Parameters

    degrees

    Amount to tilt. Positive or negative determines direction.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Shaking animation.

    Important

    Uses Animate().translate, so be aware if applying this animation along with an Animate().rotate transform.

    Declaration

    Swift

    @discardableResult
    func shake(vertical: Bool? = nil, keys: Array<Double>? = nil, then: Completion? = nil) -> Animate

    Parameters

    vertical

    If true, the animation will be a vertical shake, else it will be a horizontal shake. Default false.

    • keys: An array describing points to move to along a single axis in reference to the origin. Passing nil to this parameter will use the default [10, -10, 7, -7, 4, -4, 1, -1].
    • then: Nullable completion handler, executed after the duration of the animation.

  • Jiggle animation.

    Declaration

    Swift

    @discardableResult
    func jiggle(_ amount: Double = 1.08, _ times: Int = 7, then: Completion? = nil) -> Animate

    Parameters

    amount

    The maximum size to which the view can be jiggled. Default 1.08.

    times

    The number of times the view should jiggle. Default 7.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Declaration

    Swift

    @discardableResult
    func fadeIn(then: Completion? = nil) -> Animate

    Parameters

    then

    Nullable completion handler, executed after the duration of the animation.

  • Shrink and fade the view to a scale that’s near invisible.

    Declaration

    Swift

    @discardableResult
    func shrinkAndWink(then: Completion? = nil) -> Animate

    Parameters

    then

    Nullable completion handler, executed after the duration of the animation.

  • Set the view to a scale and opacity that’s near invisible, then grow to full size.

    Declaration

    Swift

    @discardableResult
    func growAndShow(then: Completion? = nil) -> Animate

    Parameters

    then

    Nullable completion handler, executed after the duration of the animation.

  • Flashing

    not compatible with .now() since it’s a repeating animation

    Declaration

    Swift

    @discardableResult
    func flashing(_ on: Bool) -> Animate

    Parameters

    then

    Nullable completion handler, executed after the duration of the animation.

  • Change the view layer opacity to a percentage.

    Declaration

    Swift

    @discardableResult
    func fade(to: Float, then: Completion? = nil) -> Animate

    Parameters

    to

    Fade the view layer opacity to this percentage.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Change the view layer opacity by a percentage. (e.g. If the current opacity is 0.5, .fade(by:0.5) will change the opacity to 0.25 or 0.5 * 0.5)

    Important

    Requires the view opacity to be > 0.

    Declaration

    Swift

    @discardableResult
    func fade(by: Float, then: Completion? = nil) -> Animate

    Parameters

    by

    Fade the view layer opacity by this percentage.

    then

    Nullable completion handler, executed after the duration of the animation.

  • 2D Flip animation.

    Declaration

    Swift

    @discardableResult
    func flip(vertical: Bool? = nil, then: Completion? = nil) -> Animate

    Parameters

    vertical

    If true, the animation will be a vertical flip, else it will be a horizontal flip. Default false.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Move the view origin to point [x, y] in the superviews space.

    This animation is different from .translate(to:). It moves the origin of the view frame.

    Declaration

    Swift

    @discardableResult
    func move(to: Array<CGFloat>, then: Completion? = nil) -> Animate

    Parameters

    to

    An array consisting of two CGFloat, ordered as [x, y]. This is the point to which the origin is moved. This functions differently from .translate(to:).

    then

    Nullable completion handler, executed after the duration of the animation.

  • Move the view origin by this amount [x, y] in relation to the current view origin.

    This animation is different from .translate(by:). It moves the origin of the view frame.

    Declaration

    Swift

    @discardableResult
    func move(by: Array<CGFloat>, then: Completion? = nil) -> Animate

    Parameters

    by

    An array consisting of two CGFloat, ordered as [x, y]. This is the amount by which the origin is moved.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Move the view center to point [x, y] in the superviews space.

    This animation is different from .translate(to:). It moves the center of the view frame.

    Declaration

    Swift

    @discardableResult
    func move(center to: Array<CGFloat>, then: Completion? = nil) -> Animate

    Parameters

    to

    An array consisting of two CGFloat, ordered as [x, y]. This is the point to which the center is moved. This functions differently from .translate(to:).

    then

    Nullable completion handler, executed after the duration of the animation.

  • Translate the view layer to point [x, y] in relation to the layer origin.

    This will reset all other transforms on the layer.

    This animation is different from .move(to:). It is a transform on the view layer and does not change the actual frame location.

    Important

    .translate does not work as you might expect when combined with .rotate transform. Use .move(by:) if you need to chain animations that move and rotate.

    Declaration

    Swift

    @discardableResult
    func translate(to: Array<CGFloat>, then: Completion? = nil) -> Animate

    Parameters

    to

    An array consisting of two CGFloat, ordered as [x, y]. This represents a point in relation to the view layers origin to which the layer is translated. This functions differently from .move(to:).

    then

    Nullable completion handler, executed after the duration of the animation.

  • Translate the view layer by [x, y] amount in relation to the layers current position.

    This animation is different from .move(by:). It is a transform on the view layer and does not change the actual frame location.

    Important

    .translate does not work as you might expect when combined with .rotate transform. Use .move(by:) if you need to chain animations that move and rotate.

    Declaration

    Swift

    @discardableResult
    func translate(by: Array<CGFloat>, then: Completion? = nil) -> Animate

    Parameters

    by

    An array consisting of two CGFloat, ordered as [x, y]. This represents a point in relation to the view layers origin to which the layer is translated.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Uniformly scale the view layer to an amount in relation to its original size.

    Using this animation will reset any other transforms.

    Important

    .scale(to:) values are in relation to the original scale. .scale(to: 1.5) would scale a view to 150% its original size, and .scale(to: 1) would then scale it back to it’s original size.

    Declaration

    Swift

    @discardableResult
    func scale(to: CGFloat, then: Completion? = nil) -> Animate

    Parameters

    to

    Amount to which the view layer is scaled. 1.0 will always be the original size.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Uniformly scale the view layer by an amount in relation to its current transform.

    Important

    .scale(by:) values are in relation to the current scale, which is always represented as 1.0. If you scale to 1.5, then try to scale to 1.0 there would be no change. To more easily calculate values for .scale(by:), you could use fractions instead of decimals. Scale to 3/2 to increase size by 50%, then scale to 2/3 to get back to the original size.

    Declaration

    Swift

    @discardableResult
    func scale(by: CGFloat, then: Completion? = nil) -> Animate

    Parameters

    by

    Amount by which the view layer is scaled. 1.0 will always be no change.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Rotate the view layer around its center point, to an angle in degrees in relation to its origin.

    Using this animation will reset any other transforms.

    Important

    .translate does not work as you might expect when combined with .rotate transform. Use .move(by:) if you need to chain animations that move and rotate.

    Declaration

    Swift

    @discardableResult
    func rotate(to: Double, then: Completion? = nil) -> Animate

    Parameters

    to

    Rotate the view layer to this angle in degrees. Positive or negative determines direction.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Rotate the view layer around its center point, by an amount in degrees in relation to its current transform.

    Important

    .translate does not work as you might expect when combined with .rotate transform. Use .move(by:) if you need to chain animations that move and rotate.

    Declaration

    Swift

    @discardableResult
    func rotate(by: Double, then: Completion? = nil) -> Animate

    Parameters

    by

    Rotate the view layer by this amount in degrees. Positive or negative determines direction.

    then

    Nullable completion handler, executed after the duration of the animation.

  • Invert the view layers current transform. No change if the transform is already its identity.

    Declaration

    Swift

    @discardableResult
    func invert(then: Completion? = nil) -> Animate

    Parameters

    then

    Nullable completion handler, executed after the duration of the animation.

  • Animates the view layer and frame back to its original state.

    Declaration

    Swift

    @discardableResult
    func reset(then: Completion? = nil) -> Animate

    Parameters

    then

    Nullable completion handler, executed after the duration of the animation.