Swift:添加子视图会阻止用户使用应用程序的Lottie动画



我一直在摆弄Lottie动画。我试图实现一个简短的动画,将触发一个按钮点击。我的代码基本上做了它的事情,但问题是它只做了一次。然后按钮变得无法点击,就好像有什么东西在视图的顶部,用户与之交互(至少这是我的假设)。我怎样才能继续使用Lotti动画,并使其回到应用程序的主视图,在动画后再次点击按钮?

下面是我正在使用的代码片段:

class ViewController: UIViewController {

lazy var backgroundAnimationView: AnimationView = {



let animationView = AnimationView()

view.addSubview(animationView)
animationView.translatesAutoresizingMaskIntoConstraints = false
animationView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
animationView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
animationView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
animationView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

view.addSubview(animationView)
return animationView



}()


private func playBackgroundAnimation(){
let animation = Animation.named("confettiBurst")
backgroundAnimationView.animation = animation
backgroundAnimationView.play(fromProgress: 0, toProgress: 1, loopMode: .playOnce)
}

@IBOutlet weak var button: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// createLayer()

//    who .  what    =  value

button.layer.cornerRadius = 15


}

@IBAction func rollButtonPress(_ sender: UIButton) {
playBackgroundAnimation()

button.alpha = 1


print("button tapped")
}
}

我现在使用PKHUD来实现这个功能。最初使用的是MBProgressHUD,但多年没有维护。下面的UIView extension是为了语法方便。

extension UIView {

// MARK: -
func showHud(message: String) {
HUD.dimsBackground = false

let onView = self

if (message.isEmpty == false) {
HUD.show(.labeledProgress(title: message, subtitle: nil), onView: onView)
} else {
HUD.show(.progress, onView: onView)
}
}

func hideHud() {
HUD.hide()
}

/// Only text message
func showTextHud(_ text: String) {
/*
Reference url: https://github.com/pkluz/PKHUD
*/
HUD.show(.label(text), onView: self)
}
}
// Example:
// 
@IBAction func OnBtnNext(_ sender: Any) {
// block UI with animation
self.view.showHud(message: "")
// call web api do something, and wait result 
webApi.doSomething() { [weak self] (isDone: Bool) in
// un-block UI
self?.view.hideHud()
}
}

经过深思熟虑,在Paulw11的帮助下,我设法解决了这个问题。我将把答案留在下面,供将来遇到同样烦恼的人参考。

private func playBackgroundAnimation(){
let animation = Animation.named("confettiBurst")
backgroundAnimationView.animation = animation

backgroundAnimationView.play{_ in 
self.backgroundAnimationView.isHidden = false

}

backgroundAnimationView.play(fromProgress: 0, toProgress: 1, loopMode: .playOnce)

backgroundAnimationView.play { (finished) in
self.backgroundAnimationView.isHidden = true
}

}

我只是添加了一些代码,在调用开始时隐藏animationView,然后隐藏它。这招很管用。现在动画关闭了,用户仍然可以点击背景中的所有按钮。

最新更新