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

UIKit don't have createDotLottieView() method #24

Closed
PollyVern opened this issue Apr 11, 2024 · 6 comments
Closed

UIKit don't have createDotLottieView() method #24

PollyVern opened this issue Apr 11, 2024 · 6 comments

Comments

@PollyVern
Copy link

Good afternoon. There is no method for UIKit -> createDotLottieView. I didn't find it in the library documentation either. How can I add animation?

Снимок экрана 2024-04-11 в 13 26 10
@samuelOsborne
Copy link
Member

hey @PollyVern can you try

dotLottieAnimation.view()

Think the docs need updating

@PollyVern
Copy link
Author

@samuelOsborne I try this, but Xcode say: Ambiguous write of 'view()'

Снимок экрана 2024-04-11 в 13 40 24

@samuelOsborne
Copy link
Member

thats looks like more of a swiftui style of doing things, I setup my uikit view like this:

class AnimationViewController: UIViewController {
    var simpleVM = DotLottieAnimation(webURL: "https://lottie.host/link.lottie", config: AnimationConfig(autoplay: true, loop: false))

    override func viewWillAppear(_ animated: Bool) {
        let dotLottieView = simpleVM.view()
        view.addSubview(dotLottieView)
    }
}

@PollyVern
Copy link
Author

@samuelOsborne I add your code in project. The situation persists.
I'm importing import DotLottie

Снимок экрана 2024-04-11 в 13 46 09

@samuelOsborne
Copy link
Member

@PollyVern I think you might need to create a DotLottieAnimationView() and pass your model instead.

This is what i'm using:

//
//  ViewController.swift
//  DotLottieIosUIKitTest
//
//  Created by Sam on 21/11/2023.
//

import UIKit
import DotLottie

class dlo : Observer {
    func onLoadError() {
    }
    
    func onComplete() {
        print("on complete")
    }
    
    func onFrame(frameNo: Float) {
//        print("on frame")
    }
    
    func onLoad() {
        print("on load")
    }
    
    func onLoop(loopCount: UInt32) {
        print("on loop \(loopCount)")
    }
    
    func onPause() {
        print("on pause")
    }
    
    func onPlay() {
        print("on play")
    }
    
    func onRender(frameNo: Float) {
//        print("on render \(frameNo)")
    }
    
    func onStop() {
        print("on stop")
    }
    
}

class ViewController: UIViewController {
    var dotLottieView: DotLottieAnimationView!
    
    var observer = dlo()
    
    var simpleVM = DotLottieAnimation(webURL: URLBook.chickLottie, config: AnimationConfig(autoplay: true, loop: true))
    
    override func viewWillAppear(_ animated: Bool) {
        let loaderView: DotLottieAnimationView = simpleVM.view()
        loaderView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(loaderView)
        loaderView.frame = view.bounds

        
        generatePlayButton()
        generatePauseButton()
        generateStopButton()
    }
    
    func generatePauseButton() {
        let button = UIButton(type: .system)
        
        button.setTitle("Pause Animation", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .blue
        
        // Add an action for the button tap event
        button.addTarget(self, action: #selector(pauseAnimation), for: .touchUpInside)
        
        // Set button frame or constraints if needed
        button.frame = CGRect(x: 100, y: 600, width: 200, height: 50)
        view.addSubview(button)
    }
    
    func generatePlayButton() {
        let button = UIButton(type: .system)
        
        button.setTitle("Play Animation", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .blue
        
        // Add an action for the button tap event
        button.addTarget(self, action: #selector(playAnimation), for: .touchUpInside)
        
        // Set button frame or constraints if needed
        button.frame = CGRect(x: 100, y: 700, width: 200, height: 50)
        view.addSubview(button)
    }
    
    func generateStopButton() {
        let button = UIButton(type: .system)
        
        button.setTitle("Stop Animation", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .blue
        
        // Add an action for the button tap event
        button.addTarget(self, action: #selector(stopAnimation), for: .touchUpInside)
        
        // Set button frame or constraints if needed
        button.frame = CGRect(x: 100, y: 800, width: 200, height: 50)
        view.addSubview(button)
    }
    
    @objc func pauseAnimation() {
        simpleVM.pause()
        simpleVM.setBackgroundColor(bgColor: .yellow)
    }
    
    @objc func stopAnimation() {
        simpleVM.stop()
    }
    
    @objc func playAnimation() {
        simpleVM.play()
        simpleVM.setBackgroundColor(bgColor: .magenta)
    }
    
}


@PollyVern
Copy link
Author

Yes, it worked! The trick was to explicitly assign the type:

let loaderView: DotLottieAnimationView = simpleVM.view()

Thanks

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