LWKLoadingContext

public class LWKLoadingContext : NSObject

The interface for reading Lightwell screen documents and generating native elements – UIViews and CAAnimations.

Exported projects from Lightwell include screen data and sliced images prepped in an asset catalog, vector and audio assets stored in an Assets folder, and the LightwellKit framework that includes all of the documented functionality. Makes sure to include all of them in your Xcode project.

After including the screen documents, a LWKLoadingContext can be initialized to read out the content.

let loadingContext = LWKLoadingContext(screenName: "my awesome screen")

The loading context will then lazy load views and animations that correspond to the layers and actions defined in the Lightwell editor. The LWKLoadingContext provides default behavior to translate the document’s content into UIViews and CAAnimations. However, if you’d like to do something custom the loading context provides hooks to override the default behavior. See LWKLoadingContext.loadViews(usingGenerator:) and LWKLoadingContext.loadAnimations(usingGenerator:)

A basic implementation of the visual elements into a view controler might look like this:

class MyViewController: UIViewController {
   override func viewDidLoad() {
       super.viewDidLoad()

       // Do any additional setup after loading the view.
       let loadingContext = LWKLoadingContext(screenName: "my awesome screen")
       loadingContext?.views.forEach { self.view.addSubview($0) }
   }
}

If you are also including the dynamic elements, make sure to store the context as a parameter to save on reinitializing objects. For example:

class MyViewController: UIViewController {
   let loadingContext = LWKLoadingContext(screenName: "my awesome screen")

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        loadingContext?.views.forEach { self.view.addSubview($0) }
    }

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

        if let view = self.loadingContext?.view(for: "my view") {
            if let animation = self.loadingContext?.animation(for: "my animation") {
               view.layer.add(animation, forKey: nil)
            }
        }
    }
}
  • Initializes a new LWKLoadingContext for a single screen from Lightwell.

    Declaration

    Swift

    public init?(screenName: String, in bundle: Bundle? = nil)

    Parameters

    screenName

    The filename for the archived scene excluding the file extension.

    bundle

    The bundle containing the archived scene. If the bundle is nil, the main bundle is used. By default, this parameter is nil.

    Return Value

    Either a LWKLoadingContext or nil if a document is not found.

  • Background fill for the screen.

    Declaration

    Swift

    public var screenColor: UIColor? { get }
  • Accessor for raw layer info.

    The name for the layer is the same name as the layer in Lightwell. If two layers have the same name, the most front will be returned.

    For more info about the raw layer info, see LWKLayerInfo.

    • name: The name of the layer.

    Declaration

    Swift

    public func layerInfo(for layerName: String) -> LWKLayerInfo?

    Return Value

    Either the layer info or nil if the layer doesn’t exist in the context.

  • Root level views for the loaded screen.

    If the views have not been loaded yet, this will implicitly load the views using the default behavior. If you would like something other than the default behavior make sure to call loadViews(usingGenerator:) first.

    Declaration

    Swift

    public var views: [UIView] { get }
  • Retrieve an initialized view by name.

    The name for the view is the same name as the layer in Lightwell. If two layers have the same name, the most front view will be returned.

    If the views have not been loaded yet, this will implicitly load the views using the default behavior. If you would like something other than the default behavior make sure to call loadViews(usingGenerator:) first.

    Declaration

    Swift

    public func view(for name: String) -> UIView?

    Parameters

    name

    The name for the view.

    Return Value

    Either the named view or nil if a view with the name was never loaded.

  • Change the type of view to use for the layer.

    If the views have not been loaded yet, this will change the internally initialized type when loaded via loadViews(usingGenerator:).

    If the view already has been initialized, a new view of the specified type will be initialized and replace the old view in the hierarchy.

    Declaration

    Swift

    public func setViewType(_ viewType: UIView.Type?, for name: String)

    Parameters

    viewType

    The overrided type. If nil, the default view type will be determined by the layer’s contents.

    name

    The name of the layer.

  • Load the views from the screen document.

    This function loads the views from back to front. If this will replace any views already loaded by earlier calls to loadViews(usingGenerator:) or any of the view accessors.

    By default the type of UIView loaded is depenedent on the content of the layer. Image and image sequence based layers will return a UIImageView; text based layers will return a UILabel; shape layers will return an LWKShapeView, a UIView backed by a CAShapeLayer; svg based layers are under construction. Views are positioned, sized, added to the hierarchy, and will have a CAAnimation attahced if ambient motion is specified.

    Declaration

    Swift

    @discardableResult
    public func loadViews(usingGenerator generator: ((LWKLayerInfo) -> UIView?)? = nil) -> [UIView]

    Parameters

    generator

    An optional block that takes LWKLayerInfo and returns an optional UIVIew. If the block returns nil instead of a view, children of that view will be skipped. If the block is nil, the views will be initialized using the default behavior.

    Return Value

    An array of root level views.

  • Accessor for raw animation info.

    The name for the animation is the same name as the animation in Lightwell. If two animations have the same name, the first encountered animation will be returned.

    For more info about the raw animation info, see LWKLayerInfo.

    • name: The name of the layer.

    Declaration

    Swift

    public func animationInfo(for animatinoName: String) -> LWKAnimationInfo?

    Return Value

    Either the layer info or nil if the layer doesn’t exist in the context.

  • Root level animations for the loaded screen.

    If the animations have not been loaded yet, this will implicitly load the animations using the default behavior. If you would like something other than the default behavior make sure to call loadAnimations(usingGenerator:) first.

    Declaration

    Swift

    public var animations: [CAAnimation] { get }
  • Retrieve an initialized animation by name.

    The name for the animation is the same name as the action in Lightwell. If two actions have the same name, the first encountered animation will be returned.

    If the animations have not been loaded yet, this will implicitly load the animation using the default behavior. If you would like something other than the default behavior make sure to call loadAnimations(usingGenerator:) first.

    • name: The name for the animation.

    Declaration

    Swift

    public func animation(for name: String) -> CAAnimation?

    Return Value

    Either the named animation or nil if an animation with the name was never loaded.

  • Load the animations from the screen document.

    This function loads the animations, replacing any animations already loaded by earlier calls to loadAnimations(usingGenerator:) or any of the animation accessors.

    By default, either a CAAnimationGroup or CAKeyframeAnimation will be generated. Keyframe values, CALayer property keypaths, and timing will be prepopulated.

    Note

    CAAnimations do not store any sort of target information with them. So, you will need to add the animation to the correct view when playing an animation

    if let anim = context.animation(for: "animation name") {
       view.layer.add(anim, forKey: "key")
    }
    

    Declaration

    Swift

    @discardableResult
    public func loadAnimations(usingGenerator generator: ((LWKAnimationInfo) -> CAAnimation?)? = nil) -> [CAAnimation]

    Parameters

    generator

    An optional block that takes LWKAnimationInfo and returns an optional CAAnimation. If the block returns nil instead of an animation, any children of that animation will be thrown out. However, children animations are loaded first. If the block is nil, the animations will be initialized using the default behavior.

    Return Value

    An array of root level animations.

  • Root level lightwell actions for the loaded screen.

    If the actions have not been loaded yet, this will lazy load the actions.

    Declaration

    Swift

    public var actions: [LWKAction] { get }
  • Retrieve an initialized action by name.

    The name for the action is the same name as in Lightwell. If two actions have the same name, the first encountered action will be returned.

    This actions will be lazily loaded.

    • name: The name for the action.

    Declaration

    Swift

    public func action(for name: String) -> LWKAction?

    Return Value

    Either the named action or nil if an action with the name was never loaded.