2017-06-04 6 views
1

私はSwiftを学びたいと思っています。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() 

それから、すべてのアニメーションを削除するには。

関連する問題