1
我試圖學習Swift。我使用這2個功能:Swift 3.停止UIView.animate
func getRandomColor(){
let red = Float((arc4random() % 256))/255.0
let green = Float((arc4random() % 256))/255.0
let blue = Float((arc4random() % 256))/255.0
let alpha = Float(1.0)
colours = UIColor(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)
UIView.animate(withDuration: 2, delay: 1.0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: {
self.view.backgroundColor = self.colours}, completion:nil)
}
func updateTimer(){
if seconds! == 0 {
timer.invalidate()
isTimerRunning = false
}
else {
seconds! -= 1
timerLabel.text = timeString(time: TimeInterval(seconds!))
}
if seconds! < 55 {
getRandomColor()
}
}
當秒= 0時,定時器停止,但動畫繼續。定時器停止時如何停止動畫?
嘗試將此部分取出:UIViewAnimationOptions.repeat由於每次updateTimer()運行時都運行該函數,因此不必重複該操作。 – Farini