【问题标题】:Swift: Adding the subview prevents user from using the app. Lottie AnimationsSwift:添加子视图会阻止用户使用该应用程序。洛蒂动画
【发布时间】:2021-09-01 01:07:00
【问题描述】:

我一直在搞乱 Lottie 动画。我试图实现一个简短的动画,该动画将在按钮单击时触发。我的代码基本上是做它的事情,但问题是它只做一次。然后按钮变得不可点击,就好像用户交互的视图顶部有东西(至少这是我的假设。我怎样才能继续使用 Lotti 动画并让它回到应用程序的主视图动画后按钮是否可以再次点击?

这是我正在使用的代码 sn-p:

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

}

【问题讨论】:

  • 您添加了两次动画视图,并将其添加到view 的顶部,所以您是正确的;按钮顶部有东西;你的动画视图。您可以在完成后隐藏动画视图,然后在您想再次运行动画时取消隐藏,或者您可以删除它并每次添加一个新的动画视图。
  • 你好。我已经删除了双倍动画视图调用。我试过关注其他stackoverflow问题,例如这个:stackoverflow.com/questions/30769052/…,但经过数小时的尝试,我仍然无法隐藏和取消隐藏动画。它要么根本不可见并且应用程序正常工作,要么动画可见但按钮变得不可点击。您能否通过一些简短的代码示例扩展您隐藏动画的想法?当然,如果您有空闲时间给我,我将非常感谢!
  • 我的第一个想法是在某处添加“animationView.removeFromSuperview()”,但除了 Lazy Var 之外,animationView 不在范围内。所以我唯一可以隐藏动画的地方是 Lazy var,它不起作用:/
  • 好的,很抱歉发送垃圾邮件。我已经解决了这个问题,并将在这篇文章下的单独评论中详细说明。尽管如此,非常感谢您帮助我走上正轨!如果没有你的帮助,我不会走到这一步!

标签: ios swift


【解决方案1】:

我现在使用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()
     }
}

【讨论】:

    【解决方案2】:

    经过深思熟虑并在 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
            }
            
        }
            
    

    我只是添加了一些代码,在调用开始时取消隐藏动画视图并在之后隐藏它。它就像一个魅力。现在动画消失了,用户仍然可以点击背景中的所有按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 2021-01-26
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 2011-09-09
      相关资源
      最近更新 更多