首页 > 解决方案 > 动画立即结束并且不运行 UIView.animate

问题描述

我这里的代码根本不运行。我想要一个进度条向下的动画,我从 1.0 开始进度条,我希望它从右到左变为 0。我怎样才能做到这一点?这只是运行并立即结束。谢谢

UIView.animate(withDuration: 15, animations: {

                        //if (self.answerButtonOne.isSelected == true || self.answerButtonTwo.isSelected == true || self.answerButtonThree.isSelected == true || self.answerButtonFour.isSelected == true  ){
                            //self.view.layer.removeAllAnimations()
                        //}
                    }, completion: { (Bool) in
                        if Bool == true{
                           self.disableButtons()
                            self.showRightChoice()
                            let wrong:Int = UserDefaults.standard.integer(forKey: "wrong")
                            let newWrong = wrong-1
                            UserDefaults.standard.set(newWrong, forKey: "wrong")
                            self.whichImage(newWrong: newWrong)
                            self.gameOver()
                            self.wasButtonClicked = true

                        }
                        self.progressView.setProgress(0.0, animated: true)
                    })

标签: iosswiftxcodeuiview

解决方案


    var timer = Timer()
    var totalDuration = 150.0 // to run it smooth 150/10 = 15

    override func viewDidAppear(_ animated: Bool) {

          self.timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.decrementProgress), userInfo: nil, repeats: true)
      }
      @objc func decrementProgress() {
        if totalDuration == 0 {

        }else {

            self.totalDuration -= 1
            self.progress.setProgress(0.0066 * Float(self.totalDuration) , animated: true)
        }
    }

推荐阅读