Giter VIP home page Giter VIP logo

lottieui's Introduction

LottieUI

LottieUI runing on both iOS and macOS

LottieUI allows you to use delightful Lottie animations with SwiftUI without having to give up on the familiar declarative API or the powerful customization settings from the animation framework

☑️ Requirements

  • iOS 13.0 or later
  • macOS Catalina 10.15 or later NEW (Requires LottieUI 1.1 or later)

🧑‍💻 Usage

To display an animation from a local Lottie JSON file, use the LottieView component:

LottieView("MyAnimation")

If your JSON is stored on another bundle outside your project's, you can specify the Bundle to load the animation from or provide a file path where the animation file is located:

// Loads an animation from the provided bundle
LottieView("MyAnimation", bundle: DesignSystem.Bundle.main)
// Loads an animation file from the provided path
LottieView(path: "/path/to/animation.json")

🛰 Remote animations

For remote animations, LottieUI provides AsyncLottieView, which attemps to download an animation from a remote URL and present it if successful. You can also provide views to be displayed while the donwload is in progress or if the download fails:

let url = URL(string: "https://assets3.lottiefiles.com/packages/lf20_hbdelex6.json")!

AsyncLottieView(url: url) { phase in
    switch phase {
    case .loading:
        ProgressView()
    case .error:
        ErrorView
    case .success(let lottieView):
        lottieView
    }
}

🚀 Features

LottieView allows you to take control of your animations with a set of modifiers that can be applied to a LottieView:

⏯ Playback

By default, your animation will start playing automatically. To control whether the animation should be playing, use the .play(_:) modifier:

struct ContentView: View {
    @State var isPlaying: Bool = true
    
    var body: some View {
        LottieView("MyAnimation")
            .play(isPlaying)
    }
}

Playback modifier demo

🔁 Loop Mode

To setup the Loop Mode for your animation, use .loopMode(_:)

struct ContentView: View {
    var body: some View {
        LottieView("MyAnimation")
            .loopMode(.loop)
    }
}

Speed modifier demo

🖼 Current Frame and Progress

To observe the current frame being displayed in the animation and perform an action based on it, use .onFrame(_:)

Warning To make use of the Frame/Progress observers, the animation must be using the .mainThread rendering engine. This can be set by using the .renderingEngine(_:) method

struct ContentView: View {
    var body: some View {
        LottieView("MyAnimation")
            .renderingEngine(.mainThread)
            .onFrame { _ in
                // Perform action based on current frame
            }
    }
}

To observe the progress instead, use .onProgress(_:):

struct ContentView: View {
    var body: some View {
        LottieView("MyAnimation")
            .renderingEngine(.mainThread)
            .onProgress { _ in
                // Perform action based on current progress
            }
    }
}

Progress Observer modifier demo

🏃 Speed

To set the speed of an animation, use .speed(_:):

struct ContentView: View {
    var body: some View {
        LottieView("MyAnimation")
            .speed(2.0)
    }
}

Speed modifier demo

Rendering Engine

LottieUI also supports the new RenderingEngine introduced in Lottie 3.4.0, which can greatly reduce CPU usage when displaying compatible animations

By default, LottieUI uses the .automatic, which will automatically apply the new rendering engine if an animation is compatible, but you can override it with the .renderingEngine(_:) modifier:

LottieView("MyAnimation")
    .renderingEngine(.coreAnimation)

Rendering Engine modifier demo

There are many other options available such as:

  • Limit the framerate of an animation with .play(fromFrame:to:)
  • Define the background behavior of the animation with .backgroundBehavior(_:)
  • Set the value provider for a specific keypath of the animation with .valueProvider(_: keypath:)

For more information check the included documentation in each public component and modifier

🛠 Installation

Swift Package Manager

In your project's Package.swift file, add LottieUI as a dependency:

.package(name: "LottieUI", url: "https://github.com/tfmart/LottieUI", from: "1.0.0")

lottieui's People

Contributors

lmoreton avatar mcrich23 avatar tfmart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

lmoreton mcrich23

lottieui's Issues

Add support for observers for macOS

Currently, the Mac version of LottieView do not support observation of the current progress/frame rate of an animation. For the iOS counterpart, I'm using CADisplayLink to observe the animation, but unfortunately it's not available on AppKit.

If anyone has any suggestion on how I could keep track of the animation's progress on macOS, feel free to let me know! You're also welcome to contribute with a pull request with your implementation if you're feeling extra generous.

Thanks for your help!

Possibility to bind animation name

Hey would it be possible to sort bind the animation name?

I want to use Lotties on my onboarding screen, where if the user presses next a new animation shows and starts playing center screen.

Hope to hear from you soon!

Build failing for macOS target

After just adding the package and building my macOS target it's failing with this build error. Could it be because of the syntax in lottie-ios's RenderingEngineOption.swift? Some Swift version incompatibility maybe. My environment is on Swift 5.9(swiftlang-5.9.0.128.108 clang-1500.0.40.1). Is there a recommended Swift version to use with LottieUI?

ld: Undefined symbols:
  enum case for Lottie.LottieContentMode.scaleAspectFit(Lottie.LottieContentMode.Type) -> Lottie.LottieContentMode, referenced from:
      LottieUI.WrappedAnimationNSView.init(animation: Lottie.LottieAnimation?, provider: Lottie.AnimationImageProvider?) -> LottieUI.WrappedAnimationNSView in LottieUI.o
      LottieUI.WrappedAnimationNSView.update(withEngine: Lottie.RenderingEngineOption) -> () in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.realtimeAnimationFrame.getter : CoreGraphics.CGFloat, referenced from:
      closure #1 @Swift.MainActor () -> () in LottieUI.AnimationProgressObserver.(tick in _385C634819DA2085EAADFB3078ACC442)() -> () in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.realtimeAnimationProgress.getter : CoreGraphics.CGFloat, referenced from:
      closure #1 @Swift.MainActor () -> () in LottieUI.AnimationProgressObserver.(tick in _385C634819DA2085EAADFB3078ACC442)() -> () in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.configuration.getter : Lottie.LottieConfiguration, referenced from:
      LottieUI.WrappedAnimationNSView.renderingEngine.getter : Lottie.RenderingEngineOption in LottieUI.o
      LottieUI.WrappedAnimationNSView.renderingEngine.setter : Lottie.RenderingEngineOption in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.imageProvider.getter : Lottie.AnimationImageProvider, referenced from:
      LottieUI.WrappedAnimationNSView.update(withEngine: Lottie.RenderingEngineOption) -> () in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.animationSpeed.getter : CoreGraphics.CGFloat, referenced from:
      LottieUI.WrappedAnimationNSView.speed.getter : CoreGraphics.CGFloat in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.animationSpeed.setter : CoreGraphics.CGFloat, referenced from:
      LottieUI.WrappedAnimationNSView.speed.setter : CoreGraphics.CGFloat in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.setValueProvider(_: Lottie.AnyValueProvider, keypath: Lottie.AnimationKeypath) -> (), referenced from:
      LottieUI.WrappedAnimationNSView.setValueProvider(_: Lottie.AnyValueProvider?, keypath: Lottie.AnimationKeypath?) -> () in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.backgroundBehavior.getter : Lottie.LottieBackgroundBehavior, referenced from:
      LottieUI.WrappedAnimationNSView.backgroundBehavior.getter : Lottie.LottieBackgroundBehavior in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.backgroundBehavior.setter : Lottie.LottieBackgroundBehavior, referenced from:
      LottieUI.WrappedAnimationNSView.backgroundBehavior.setter : Lottie.LottieBackgroundBehavior in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.play(completion: ((Swift.Bool) -> ())?) -> (), referenced from:
      LottieUI.WrappedAnimationNSView.play(completion: ((Swift.Bool) -> ())?) -> () in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.play(fromFrame: CoreGraphics.CGFloat?, toFrame: CoreGraphics.CGFloat, loopMode: Lottie.LottieLoopMode?, completion: ((Swift.Bool) -> ())?) -> (), referenced from:
      LottieUI.WrappedAnimationNSView.play(fromFrame: CoreGraphics.CGFloat, toFrame: CoreGraphics.CGFloat, loopMode: Lottie.LottieLoopMode, _: ((Swift.Bool) -> ())?) -> () in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.stop() -> (), referenced from:
      LottieUI.WrappedAnimationNSView.stop() -> () in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.loopMode.getter : Lottie.LottieLoopMode, referenced from:
      LottieUI.WrappedAnimationNSView.loopMode.getter : Lottie.LottieLoopMode in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.loopMode.setter : Lottie.LottieLoopMode, referenced from:
      LottieUI.WrappedAnimationNSView.loopMode.setter : Lottie.LottieLoopMode in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationView.animation.getter : Lottie.LottieAnimation?, referenced from:
      LottieUI.WrappedAnimationNSView.update(withEngine: Lottie.RenderingEngineOption) -> () in LottieUI.o
  dispatch thunk of Lottie.LottieAnimationViewBase.contentMode.setter : Lottie.LottieContentMode, referenced from:
      LottieUI.WrappedAnimationNSView.init(animation: Lottie.LottieAnimation?, provider: Lottie.AnimationImageProvider?) -> LottieUI.WrappedAnimationNSView in LottieUI.o
      LottieUI.WrappedAnimationNSView.update(withEngine: Lottie.RenderingEngineOption) -> () in LottieUI.o
  enum case for Lottie.LottieBackgroundBehavior.pause(Lottie.LottieBackgroundBehavior.Type) -> Lottie.LottieBackgroundBehavior, referenced from:
      variable initialization expression of LottieUI.LottieConfiguration.(_backgroundBehavior in _3788E42FFA4BE53E59FCB330C0655D1F) : Combine.Published<Lottie.LottieBackgroundBehavior> in LottieUI.o
      LottieUI.LottieConfiguration.init() -> LottieUI.LottieConfiguration in LottieUI.o
  enum case for Lottie.LottieLoopMode.loop(Lottie.LottieLoopMode.Type) -> Lottie.LottieLoopMode, referenced from:
      closure #1 (LottieUI.AsyncLottiePhase) -> SwiftUI._ConditionalContent<SwiftUI._ConditionalContent<SwiftUI.Text, SwiftUI.Text>, LottieUI.LottieView> in static LottieUI.SwiftUIView_Previews.previews.getter : some in LottieUI.o
  enum case for Lottie.LottieLoopMode.playOnce(Lottie.LottieLoopMode.Type) -> Lottie.LottieLoopMode, referenced from:
      variable initialization expression of LottieUI.LottieConfiguration.(_loopMode in _3788E42FFA4BE53E59FCB330C0655D1F) : Combine.Published<Lottie.LottieLoopMode> in LottieUI.o
      LottieUI.LottieConfiguration.init() -> LottieUI.LottieConfiguration in LottieUI.o
  enum case for Lottie.DecodingStrategy.dictionaryBased(Lottie.DecodingStrategy.Type) -> Lottie.DecodingStrategy, referenced from:
      default argument 1 of Lottie.LottieConfiguration.init(renderingEngine: Lottie.RenderingEngineOption, decodingStrategy: Lottie.DecodingStrategy, colorSpace: __C.CGColorSpaceRef, reducedMotionOption: Lottie.ReducedMotionOption) -> Lottie.LottieConfiguration in LottieUI.o
  enum case for Lottie.RenderingEngineOption.automatic(Lottie.RenderingEngineOption.Type) -> Lottie.RenderingEngineOption, referenced from:
      variable initialization expression of LottieUI.LottieConfiguration.(_renderingEngine in _3788E42FFA4BE53E59FCB330C0655D1F) : Combine.Published<Lottie.RenderingEngineOption> in LottieUI.o
      LottieUI.LottieConfiguration.init() -> LottieUI.LottieConfiguration in LottieUI.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Progress/Framerate observer Lottie's new Core Animation engine

As of Lottie 4.0, the current solution we have for observing animations with CVDisplayLink and CADisplayLink have stopped working when rendering animations with their new Core Animation engine.

For now, we can use the legacy Main Thread engine to use the framerate/progress observers, but we'll have to sort it out what's causing the display link to break on the newer engine

If anyone has an possible solution in mind, feel free to discuss it below. You're also very welcome to contribute with a pull request!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.