【问题标题】:Animation ends right away and doesnt run UIView.animate动画立即结束并且不运行 UIView.animate
【发布时间】:2020-08-23 09:02:09
【问题描述】:

我这里的代码根本没有运行。我想要一个进度条向下的动画,我从 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)
                    })

【问题讨论】:

    标签: ios swift xcode uiview


    【解决方案1】:
        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)
            }
        }
    

    【讨论】:

    • call self.view.layoutIfNeeded() ... 更新了我的代码,现在检查一下
    • 这让我的整个页面都动画化了,我只是想让进度条动画化,我是不是搞砸了?
    • 然后而不是 self.view.layoutIfNeeded() 调用 self.progressView.layoutIfNeeded()
    • 你能分享你的视图层次吗?我你有哪个视图progressView
    猜你喜欢
    • 2020-04-16
    • 2018-02-17
    • 2022-11-22
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多