All of the examples below assume you have access to an instance of LightwellLoadingContext available as a variable named loadingContext.

There are two methods to utilize animations constructed in Lightwell. The first is to directly use Core Animation, and the CAKefyrameAnimation and CAAnimationGroup accesible from LightwellLoadingContext.animation(for:). The second is to use HSKActions, which wrap Core Animation and adding simpler run semantics and additional targeting support.

CAAnimation

To retrieve an animation you can either select it from a loading context by name:

// Get a single animation
let animation = loadingContext.animation(for: "animation name")

Or select all of them:

// Get a single animation
let animation = loadingContext.animation(for: "animation name")

To run a CAAnimation use CALayer.add(:forKey:).

// Get a single animation
if let animation = loadingContext.animation(for: "animation name") {
    viewToAnimate.layer.add(animation)
}

To stop an animation running call CALayer.removeAllAnimations():

if let animation = loadingContext.animation(for: "animation name") {
    viewToAnimate.layer.add(animation)
}
...
// Stop all animations
viewToAnimate.layer.removeAllAnimations()

Or remove a specific animation using the key parameter and CALayer.removeAnimation(forKey:):

if let animation = loadingContext.animation(for: "animation name") {
    viewToAnimate.layer.add(animation, forKey: "unique animation key")
}
...
// Stop a specific animation
viewToAnimate.layer.removeAnimation(forKey: "unique animation key")

HSKAction

Alternatively HSKAction provides sematics to fully configure an animation, including targets, before playing, and a simple interface to control playback using HSKAction.run() and HSKAction.stop()

To retrieve an action you can select it from a loading context by name:

// Get a single action
let action = loadingContext.action(for: "animation name")

To run any action you can call HSKAction.run():

// Get a single action
let action = loadingContext.action(for: "animation name")
// Play action
action?.run()

Or using optional chaining:

// Get and play action
loadingContext.action(for: "animation name")?.run()

To stop an action just call HSKAction.stop():

// Get a single action
let action = loadingContext.action(for: "animation name")
// Play action
action?.run()
...
// Stop action
action?.stop()

To change the targets of an HSKCAAnimation action, assing into the HSKCAAnimation.targets property:

// Get an animation action
let animationAction = loadingContext.action(for: "animation name") as? HSKCAAnimation
// Update targets action
action?.targets = [viewToAnimate]

A single HSKCAAnimation can support multiple targets:

// Get an animation action
let animationAction = loadingContext.action(for: "animation name") as? HSKCAAnimation
// Update targets action
action?.targets = [viewToAnimate, otherViewToAnimate]

For more advanced usage of actions see HSKAction.

The API is currently in beta and everything inside of these docs will be changing to improve the SDK. If there is a change you’d like to see or have any feedback, please email us at dev@lightwell.pro.

If you have any questions please get in touch with us via email or chat with us on lightwell.pro!