Class

Animation

The Animation class encapsulates the details of a specific animation, including timing, the layer being animated, as well as methods for playback and control.

You should never have to interact with this class. Any interaction should be with the Timeline object, which has similar controls that can be applied to all its associated layers.


Declaration

open class Animation: NSObject, CAAnimationDelegate

Overview

The Animation class represents an object that encapsulates the animations for a specific layer. It is an interface for all CAKeyframeAnimation objects associated with the layer, and provides functionality for playback and control of those objects.

Topics

Properties

keyframeAnimations

Key frame animations which animate the properties of layer.

fileprivate var keyframeAnimations: [CAKeyframeAnimation]

This is populated during initialization.

layer

The CALayer whose properties are animated.

open var layer: CALayer

autoreverses

Specifies whether or not the animation should automatically reverse upon ending.

var autoreverses: Bool

repeatCount

Determines the number of times the animation will repeat.

May be fractional. If the repeatCount is 0, it is ignored.

Setting this property to greatestFiniteMagnitude will cause the animation to repeat forever.

var repeatCount: Float

time

The current time of the animation. i.e. what time is being displayed.

var time: TimeInterval {
    return layer.timeOffset
}

playing

Returns a bool indicating whether the animation is currently playing.

The value returned will be true if the animation is playing.

var playing: Bool {
    return layer.speed != 0.0
}

reversed

Returns the reversed version of the animation, by creating a new copy with a set of individually reversed keyframe animations.

var reversed: Animation {
    let reversedKeyFrameAnimations = keyframeAnimations.map { $0.reversed }
    return Animation(layer: layer, keyframeAnimations: reversedKeyFrameAnimations)
}

Initialization

Initialization requires a CALayer and an array of CAKeyframeAnimation objects. Optionally, you can specify if the animation should reverse and/or repeat.

init(…)

    public init(layer: CALayer, keyframeAnimations: [CAKeyframeAnimation], autoreverses: Bool = false, repeatCount: Float = 0) {
        self.layer = layer
        self.keyframeAnimations = keyframeAnimations
        self.autoreverses = autoreverses
        self.repeatCount = repeatCount

        super.init()
        keyframeAnimations.forEach(configure)
        reset()
    }

This method configures each animation and then resets itself – guaranteeing that the animation will play from the beginning.

Configuration

configure()

An internal method for setting standard Core Animation options required to properly playback Flow animations.

private func configure(keyframeAnimation: CAKeyframeAnimation) {
    keyframeAnimation.delegate = self
    keyframeAnimation.isRemovedOnCompletion = false
    keyframeAnimation.fillMode = .both
    keyframeAnimation.autoreverses = autoreverses
    keyframeAnimation.repeatCount = repeatCount
}

Playback

play()

Initiates playback. Resumes the animation from where it was paused.

open func play() {
    let pausedTime = layer.timeOffset
    layer.speed = 1.0
    layer.timeOffset = 0.0
    layer.beginTime = 0.0
    let timeSincePause = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
    layer.beginTime = timeSincePause
}

pause()

Pauses the animation, and offsets the layer timing to the current paused time.

open func pause() {
    let pausedTime = layer.convertTime(CACurrentMediaTime(), from: nil)
    offset(to: pausedTime)
}

Driving

offset(to:)

Offsets the animation to a specific time. Pass this method a value measured in seconds.

open func offset(to time: TimeInterval) {
    layer.speed = 0.0
    layer.timeOffset = time
}

Reset

reset()

Resets the animation to time 0.

The best approach for resetting animations using Core Animation, is to remove all existing animations on a layer and then re-add new versions animations whose times are set to 0.

open func reset() {
    CATransaction.suppressAnimations {
        layer.removeAllAnimations()
        layer.beginTime = 0
        offset(to: 0)

        for keyframeAnimation in keyframeAnimations {
            layer.setValue(keyframeAnimation.values?.first, forKeyPath: keyframeAnimation.keyPath!)
        }

        addAllAnimations()
        layer.speed = 0
    }
}

addAllAnimations()

Adds all the animations to layer so they can be played.

private func addAllAnimations() {
    for keyframeAnimation in keyframeAnimations {
        layer.add(keyframeAnimation, forKey: keyframeAnimation.keyPath)
    }
}

Delegate

animationDidStop(…)

This method handles looping the animation once it has stopped. It takes into consideration both the repeatCount and autoreverses variables.

public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    if flag {
        let time = autoreverses ? 0 : (keyframeAnimations.first?.duration ?? 0)
        offset(to: time)
    }
}
background Made with Flow.
underscore Made with Flow.
line2 Made with Flow.
line1 Made with Flow.
circle Made with Flow.
hit Made with Flow.

result(s) found for “”.