ios - 动画立即结束并且不运行 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)
})
解决方案
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)
}
}
推荐阅读
- collections - 在对复合 json 进行排序时,不能强制转换为 java.lang.Comparable
- excel - 无法在 Excel VBA 中用字符填充数组
- javascript - 在 Angular 中渲染基于时间的 Observables 而不会检测到压倒性的变化
- javascript - How to use a variable declared in super method of parent Class inside same metod of inherited Class
- sql - Msg 207 在 WHERE 子句中使用 CTE 的列无效
- python - 使用两个数据帧的值执行计算
- machine-learning - How can I create a bot able to ask for multiple values in a single prompt?
- ios - Headless webview
- python - Print list without brackets or commas in python
- json - jq- 在一个值上合并两个 json 文件