End or Dismiss Lottie Animation View if playOnce is finished in Swift

Viewed 2186

I already tried both loadingview.removeFromSuperView and loadingView.isHidden = true

Yes, it removes or hides the view, but I can't click on my root view anymore.

I also tried animatonview.background = .forceFinish, but doesn't do the job.

import UIKit
import Lottie

class LoadingAnimationView: UIView {

@IBOutlet weak var loadingView: UIView!
let animationView = AnimationView()

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func awakeFromNib() {
    super.awakeFromNib()
} 

func loadAnimation() {
        let animation = Animation.named("success")

        animationView.animation = animation
        animationView.contentMode = .scaleAspectFill

        loadingView.addSubview(animationView)
        animationView.backgroundBehavior = .pauseAndRestore
        animationView.translatesAutoresizingMaskIntoConstraints = false
        animationView.topAnchor.constraint(equalTo: loadingView.layoutMarginsGuide.topAnchor).isActive = true
        animationView.leadingAnchor.constraint(equalTo: loadingView.leadingAnchor, constant: 0).isActive = true

        animationView.bottomAnchor.constraint(equalTo: loadingView.bottomAnchor).isActive = true
        animationView.trailingAnchor.constraint(equalTo: loadingView.trailingAnchor, constant:0).isActive = true
        animationView.setContentCompressionResistancePriority(.fittingSizeLevel, for: .horizontal)
        
        animationView.play(fromProgress: 0,
                           toProgress: 1,
                           loopMode: .playOnce,
                           completion: { (finished) in
                            if finished {
                              print("Animation Complete")
                              //please put solution here? dismiss or end loadingView or animationView
                            } else {
                              print("Animation cancelled")
                            }
        })
    }

EDIT 2:

I'm using the loadingView when the success message is received or 200.

func goOnlineMode(){
        APIManager.sharedInstance.fetchServerStatus(completion: { data, error in
            if error != nil{
                print("Connection Failed")
            } else {
                if data?.status == 200 || data?.msg == "success" {
                    print("connected")
                    loadAnimation(true)
                    self.setCloudStateValue(value: true)
                    self.vc.cloudstateChecker()
                } else {
                    print("fail to connect")
                }
            }
        })
    }
}

this is my function loading boolean in loadAnimation for Loading the xib.

func loadAnimation(_ display: Bool) {
    if (display) {
        let window = UIApplication.shared.keyWindow!
        
        if Singleton.animationView == nil {
            if let view = Bundle.main.loadNibNamed("LoadingAnimationView", owner: window, options:nil)![0] as? LoadingAnimationView {
                
                Singleton.animationView = view
                Singleton.animationView?.frame.size = CGSize(width: window.bounds.width, height: window.bounds.height)
                window.addSubview(Singleton.animationView!)
                window.layoutIfNeeded()
                Singleton.animationView?.loadAnimation()
                Singleton.animationView?.translatesAutoresizingMaskIntoConstraints = false
                Singleton.animationView?.leftAnchor.constraint(equalTo: window.leftAnchor).isActive = true
                Singleton.animationView?.rightAnchor.constraint(equalTo: window.rightAnchor).isActive = true
                Singleton.animationView?.topAnchor.constraint(equalTo: window.topAnchor, constant:-60).isActive = true
                Singleton.animationView?.bottomAnchor.constraint(equalTo: window.bottomAnchor).isActive = true
                window.layoutIfNeeded()
            }
        }
    } else {
        if (Singleton.animationView != nil) {
            Singleton.animationView?.removeFromSuperview()
            Singleton.animationView = nil
        }
    }
}
2 Answers

Try with this: Swift 5

animationView.play { (finished) in
    animationViewNewOrder!.isHidden = true
}

I solved my problem by using NotificationCenter

Swift 4.2

Add this NotificationCenter Observer in your MainViewController, and also register a Notification.Name to your Constants

NotificationCenter.default.addObserver(self, selector: #selector(removeAnimation(notification:)), name: HIDE_ANIMATION, object: nil)
}

also add this together with your observer

@objc func removeAnimation(notification:NSNotification) {
        loadingAnimation(false)
}

I put this Notification Post in my newly created hideAnimation function in LoadingAnimationView.

    func hideAnimation() {
        NotificationCenter.default.post(name: Notification.Name(HIDE_ANIMATION.rawValue), object: nil)
        loadingView.removeFromSuperview()
}

and put the hideAnimation function to your completion finish.

Related