如果 playOnce 在 Swift 内完成,则结束或关闭 Lottie 动画视图

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

我已经尝试了 loadingview.removeFromSuperViewloadingView.isHidden = true

是的,它删除或隐藏了视图,但我无法再单击我的根视图。

我也尝试了 animatonview.background = .forceFinish,但没有成功。

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")
                            }
        })
    }

编辑 2:

收到成功消息或 200 时,我正在使用 loadingView。

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")
                }
            }
        })
    }
}

这是我在 loadAnimation 中加载布尔值的函数,用于加载 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
        }
    }
}

我使用 NotificationCenter 解决了我的问题

Swift 4.2

在您的 MainViewController 中添加此 NotificationCenter Observer,并向您的 Constants

注册一个 Notification.Name
NotificationCenter.default.addObserver(self, selector: #selector(removeAnimation(notification:)), name: HIDE_ANIMATION, object: nil)
}

也将此与您的观察者一起添加

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

我把这个通知 Post 放在我在 LoadingAnimationView 中新创建的 hideAnimation 函数中。

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

并把 hideAnimation 函数放到你完成的地方。

试试这个: Swift 5

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