Usage

The following are samples of SDK usage.

Reading of Lightwell content goes through the LWKLoadingContext class, which manages reading the raw document into accesible info, and convenience initialization of native elements. The initializer for the loading context takes a file name. Note that the initialization will fail if the file cannot be found of if the data has been corrupted.

let loadingContext = LWKLoadingContext("file name")

Object links, like targets of an action, depend on being initialized by the same context. So it is generally good practice to either get all of the views and animations out from a single loading context at once, or to store a loading context as a property.

class MyClass {
    let loadingContext = LWKLoadingContext("file name")
}

Since the initialization of the loading context will only fail if the file cannot be found, or if the data has been corrupted, after testing to make sure it runs once it can be safely unwrapped like a paramter initialized from a nib file. Although, we suggest maintaining Swift best practices with optionals.

class MyClass {
    let loadingContext: LWKLoadingContext! = LWKLoadingContext("file name")
}

Accessing objects from the loading context can be done through a series of accessor properties and functions for views:

// Grab all views
let views = loadingContext?.views
// Get a specific view
let view = loadingContext?.view(for: "view name")

animations:

// Grab all animations
let views = loadingContext?.animations
// Get a specific animation
let animation = loadingContext?.animation(for: "animation name")

actions:

// Get a specific action
let action = loadingContext?.action(for: "action name")

To add all of the contents from a Lightwell screen to a UIViewController you can utilize the viewDidLoad() function:

class MyViewController: UIViewController {
    // Load document
    let loadingContext: LWKLoadingContext = LWKLoadingContext("file name")

    override func viewDidLoad() {
        super.viewDidLoad()
        // Add views to hierarchy
        loadingContext?.views.forEach { self.view.addSubview($0) }
    }
}

To trigger an animation you can utilize CALayer‘s add(_:forKey:) function:

// Get a specific animation
if let animation = loadingContext?.animation(for: "animation name") {
    view.layer.add(animation)
}

Or in the building onto the UIViewController example, responding to a tap anywhere:

class MyViewController: UIViewController {
    // Load document
    let loadingContext: LWKLoadingContext = LWKLoadingContext("file name")

    override func viewDidLoad() {
        super.viewDidLoad()
        // Add views to hierarchy
        loadingContext?.views.forEach { self.view.addSubview($0) }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)

        // Get the animation
        if let animation = loadingContext?.animation(for: "animation name") {
            // Play the animation
            loadingContext?.view(for: "target view name")?.layer.add(animation)
        }
    }
}

To simplify to the interface for triggering animations, and to add support for storing target data with the animation and grouping animations targeting different views, we’ve added self-managed action semantics. The same animation example from above can also be implemented as:

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

Or in the context of the view controller:

class MyViewController: UIViewController {
    // Load document
    let loadingContext: LWKLoadingContext = LWKLoadingContext("file name")

    override func viewDidLoad() {
        super.viewDidLoad()
        // Add views to hierarchy
        loadingContext?.views.forEach { self.view.addSubview($0) }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)

        // Get and run the animation targeting the layers specified in the Lightwell editor
        loadingContext?.action(for: "animation name")?.run()
    }
}

If you’re wondering how to do something, or would like more detail with any of the examples, let us know!

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!