2017-06-04 222 views
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时,定时器停止,但动画继续。定时器停止时如何停止动画?

+0

尝试将此部分取出:UIViewAnimationOptions.repeat由于每次updateTimer()运行时都运行该函数,因此不必重复该操作。 – Farini

回答

1

在你:

if seconds! == 0 { 
    timer.invalidate() 
    isTimerRunning = false 
} 

添加此行:

self.view.layer.removeAllAnimations() 

从而除去所有动画。