Lottie Animation:即使在单击 AnimatedButton 后也播放动画

Lottie Animation: play animation even after AnimatedButton is clicked

我正在使用来自 Lottie 框架的 AnimatedButton,它工作正常,但我遇到的唯一问题是,当我想要按钮始终保持动画效果,即使在点击后也是如此。

let middleButton = AnimatedButton()
    
 middleButton.frame.size = CGSize(width: 80, height: 80)
    
 middleButton.animation = Animation.named("eye")       
 middleButton.animationView.play()
 middleButton.animationView.loopMode = .autoReverse
 middleButton.animationView.backgroundBehavior = .pauseAndRestore
 middleButton.addTarget(self, action: #selector(self.middleButtonAction), for: .touchUpInside)

你可以试试下面吗?

let middleButton = AnimatedButton()
    
 middleButton.frame.size = CGSize(width: 80, height: 80)
    
 middleButton.animation = Animation.named("eye")       
 middleButton.animationView.loopMode = .autoReverse
 middleButton.animationView.backgroundBehavior = .pauseAndRestore
 middleButton.addTarget(self, action: #selector(self.middleButtonAction), for: .touchUpInside)
middleButton.animationView.play()

我设法通过在每次按下按钮时初始化 lottie 动画来解决这个问题:

@objc func didTapButton(sender: UIButton) {
   
    // Animation logic
    middleButton.animation = Animation.named("eye")
    middleButton.animationView.loopMode = .autoReverse
    middleButton.animationView.backgroundBehavior = .pauseAndRestore
    middleButton.animationView.play()
    
    didTapButton?()
}