LottieFiles / dotlottie-ios

.lottie player for iOS
MIT License
35 stars 4 forks source link

UIKit don't have createDotLottieView() method #24

Closed PollyVern closed 2 months ago

PollyVern commented 2 months ago

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 commented 2 months ago

hey @PollyVern can you try

dotLottieAnimation.view()

Think the docs need updating

PollyVern commented 2 months ago

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

Снимок экрана 2024-04-11 в 13 40 24
samuelOsborne commented 2 months ago

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 commented 2 months ago

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

Снимок экрана 2024-04-11 в 13 46 09
samuelOsborne commented 2 months ago

@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 commented 2 months ago

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

let loaderView: DotLottieAnimationView = simpleVM.view()

Thanks