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

如果在Swift中完成playOnce,则结束或关闭Lottie动画视图,swift,lottie,lottie-ios,Swift,Lottie,Lottie Ios,我已经尝试了以下代码loadingview.removeFromSuperView和loadingview.isHidden=true 是的,它会删除或隐藏视图,但我无法再单击根视图 我还尝试了animatonview.background=.forceFinish,但没有成功 import UIKit import Lottie class LoadingAnimationView: UIView { @IBOutlet weak var loadingView: UIView! let

我已经尝试了以下代码loadingview.removeFromSuperView和loadingview.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:

当收到或收到成功消息时,我正在使用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的函数。
我用通知中心解决了我的问题

Swift 4.2

在MainViewController中添加此NotificationCenter观察者,并在常量中注册Notification.Name

NotificationCenter.default.addObserver(self, selector: #selector(removeAnimation(notification:)), name: HIDE_ANIMATION, object: nil)
}
还可以将其与您的观察者一起添加

@objc func removeAnimation(notification:NSNotification) {
        loadingAnimation(false)
}
我将此通知张贴在LoadingAnimationView中新创建的hideAnimation函数中

    func hideAnimation() {
        NotificationCenter.default.post(name: Notification.Name(HIDE_ANIMATION.rawValue), object: nil)
        loadingView.removeFromSuperview()
}
并将hideAnimation函数置于完成状态。

尝试以下操作: 斯威夫特5


您还可以提供loadingView以及如何使用它的上下文吗?您好,我更新了上面的上下文。您好,您尝试过animationView.stop吗?是的,我尝试过,但仍然没有放弃。
    func hideAnimation() {
        NotificationCenter.default.post(name: Notification.Name(HIDE_ANIMATION.rawValue), object: nil)
        loadingView.removeFromSuperview()
}
animationView.play { (finished) in
    animationViewNewOrder!.isHidden = true
}