Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lottie 4.0.1 issue under iOS 16.2 in SwiftUI #1891

Closed
iKK001 opened this issue Jan 2, 2023 · 2 comments
Closed

Lottie 4.0.1 issue under iOS 16.2 in SwiftUI #1891

iKK001 opened this issue Jan 2, 2023 · 2 comments

Comments

@iKK001
Copy link

iKK001 commented Jan 2, 2023

Which Version of Lottie are you using?

Lottie 4.0.1

Expected Behavior

On my previous Lottie version 3.4.1, everything was fine. But now with 4.0.1 my code no longer compiles.

I am using SwiftUI under iOS 16.2, XCode 14.2 and Swift 5.7.2

Here is my code of the LottieView under SwiftUI

import SwiftUI
import Lottie

struct LottieView: UIViewRepresentable {
    
    var lottieFile: String
    var loopMode: LottieLoopMode = .playOnce
    var animationView = AnimationView()
    
    func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
        let view = UIView()
        
        animationView.animation = Animation.named(lottieFile)
        animationView.contentMode = .scaleAspectFill
        animationView.loopMode = loopMode
        
        animationView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(animationView)
        
        NSLayoutConstraint.activate([
            animationView.widthAnchor.constraint(equalTo: view.widthAnchor),
            animationView.heightAnchor.constraint(equalTo: view.heightAnchor)
        ])
        
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieView>) {
        animationView.play()
    }
}

Actual Behavior

Here a screenshot of the error messages when compiling under Lottie 4.0.1

(it seems to no longer find the AnimationView() for some reason.

What do I need to change when using Lottie 4.0.2 under iOS 16.2 in SwiftUI ??

Screenshot 2023-01-02 at 22 58 09

@iKK001
Copy link
Author

iKK001 commented Jan 2, 2023

Ah I think I've found out: When compiling under Lottie 3.5.0 I get at least the information that AnimationView was deprecated and it should rather be LottieAnimationView instead.

I tried the same under Lottie 4.0.1 and it worked. (i.e. replacing AnimationView with LottieAnimationView and also replacing Animation with LottieAnimation, as shown below.

Would have been nice if the deprecation-information would still show under 4.0.1.

import SwiftUI
import Lottie

struct LottieView: UIViewRepresentable {
    
    var lottieFile: String
    var loopMode: LottieLoopMode = .playOnce
    var animationView = LottieAnimationView()
    
    func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
        let view = UIView()
        
        animationView.animation = LottieAnimation.named(lottieFile)
        animationView.contentMode = .scaleAspectFill
        animationView.loopMode = loopMode
        
        animationView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(animationView)
        
        NSLayoutConstraint.activate([
            animationView.widthAnchor.constraint(equalTo: view.widthAnchor),
            animationView.heightAnchor.constraint(equalTo: view.heightAnchor)
        ])
        
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieView>) {
        animationView.play()
    }
}

@calda
Copy link
Member

calda commented Jan 3, 2023

Closing this issue since you found the workaround. As you noticed we renamed Animation to LottieAnimation in 4.0 to resolve a conflict with SwiftUI.Animation. Unfortunately we can't add a deprecation message since something like @deprecated typealias Animation = LottieAnimation would still conflict with SwiftUI.Animation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants