2015-10-17 7 views
6

私はこれは私がこれまでに(それは私がちょうど2を指定したときに、私がしたい正確に何しているものである私のiOSアプリの背景色が秒迅速な背景色のアニメーションループ

のx量の上に4色の間で変更したいです色)

ループを無限に実行するアニメーションも必要です。

ViewController.swift

UIView.animateWithDuration(X.0, animations: { 
     // Color 1 
     self.view.backgroundColor = UIColor(rgba) 
     // Color 2 
     self.view.backgroundColor = UIColor(rgba) 
     // Color 3 
     self.view.backgroundColor = UIColor(rgba) 
     // Color 4 
     self.view.backgroundColor = UIColor(rgba) 

    }) 
+0

私の解決策を試してください。アニメーション補完ブロックを使用することは、タイマーを実行するよりも常に優れています。 – Abhinav

答えて

12

これを試してみてください:

UIView.animateWithDuration(1.0, animations: {() -> Void in 
    self.view.backgroundColor = UIColor.blackColor() 
    }) { (Bool) -> Void in 
     UIView.animateWithDuration(1.0, animations: {() -> Void in 
      self.view.backgroundColor = UIColor.greenColor() 
      }, completion: { (Bool) -> Void in 
       UIView.animateWithDuration(1.0, animations: {() -> Void in 
        self.view.backgroundColor = UIColor.grayColor() 
        }, completion: { (Bool) -> Void in 
         UIView.animateWithDuration(1.0, animations: {() -> Void in 
          self.view.backgroundColor = UIColor.redColor() 
          }, completion:nil) 
       }) 
     }) 
} 

あなたが連続繰り返しアニメーションをしたい場合は、これを試してみる:コードの下

UIView.animateWithDuration(2, delay: 0.0, options:[UIViewAnimationOptions.Repeat, UIViewAnimationOptions.Autoreverse], animations: { 
    self.view.backgroundColor = UIColor.blackColor() 
    self.view.backgroundColor = UIColor.greenColor() 
    self.view.backgroundColor = UIColor.grayColor() 
    self.view.backgroundColor = UIColor.redColor() 
}, completion: nil) 
+0

woa that awesome! – user3390658

+0

2番目の解決策の唯一の問題は、最初の2つの色をスキップし、灰色と赤色の間で切り替えることです。 – user3390658

+0

遷移が実際には速すぎて表示されません。コンプリートブロックを使用して、オプション2を使いたい場合は、必要に応じて再生時間を調整することができます。 – Abhinav

2

あなたはNSTimerを使用する必要があります:あなたが行われたときにタイマーを無効に忘れないでください:

let timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "update", userInfo: nil, repeats: true) 

func update() { 
    let nextCollor = getNextColor() 
    UIView.animateWithDuration(X.0, animations: { 
     self.view.backgroundColor = nextCollor 
    }) 
} 

func getNextColor() -> UIColor { 
    let currentColor = self.view.backgroundColor 

    if currentColor == smaple1 { 
     return UIColor.redColor() 
    } else if currentColor == smaple2 { 
     return UIColor.grayColor() 
    } else { 
     return UIColor.whiteColor() 
    } 
} 

NSTimer.scheduledTimerWithTimeIntervalは、あなたのコードは、5秒ごとに

PSを実行しますそれと。そのためにtimer.invalidate()に電話するだけです。それ以外の場合は、クラッシュを取得します。

+0

よろしくお願いします。私は素早く動くことができます。getNextColor関数はどのように見えますか? – user3390658

+0

@ user3390658アップデートをチェックしてください。それは最高の解決策ではありません:) – Arsen

3

はアニメーションビューとユーザーとの対話を有効に維持するのに役立ちます。ランダムカラー生成(必要に応じて使用)。

UIView.animateWithDuration(7, delay: 1, options: 
         [UIViewAnimationOptions.AllowUserInteraction, 
          UIViewAnimationOptions.Repeat, 
          UIViewAnimationOptions.Autoreverse], 
     animations: { 
      self.yourView.backgroundColor = self.randomColor() 
      self.yourView.backgroundColor = self.randomColor() 
     }, completion:nil) 

func randomColor() -> UIColor { 
     let randomRed:CGFloat = CGFloat(drand48()) 
     let randomGreen:CGFloat = CGFloat(drand48()) 
     let randomBlue:CGFloat = CGFloat(drand48()) 

     return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, 
         alpha: 1.0) 
    }