2015-10-17 16 views
6

Chcę kolor tła mojego app iOS zmienić pomiędzy czterema kolorami nad x ilość sekundSwift tło pętla animacji kolor

To, co mam tak daleko (robi dokładnie to, co chcę, kiedy mogę określić tylko 2 kolory)

Potrzebuję również animacji, aby uruchomić w pętli w nieskończoność.

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

Proszę spróbować z moim rozwiązaniem. Używanie bloków ukończenia animacji jest zawsze lepsze niż uruchamianie stopera. – Abhinav

Odpowiedz

12

Spróbuj tego:

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) 
       }) 
     }) 
} 

W przypadku, gdy chcesz ciągłą powtarzający animację, spróbuj tego:

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 to niesamowite! – user3390658

+0

Jedyny problem z drugim rozwiązaniem polega na tym, że pomija on dwa pierwsze kolory, po prostu przełącza się między szarą i czerwoną. – user3390658

+0

Przejście jest zbyt szybkie, aby zobaczyć zmianę. Możesz użyć bloku ukończenia i grać z czasem, aby dopasować się do twoich potrzeb, jeśli chcesz iść z opcją 2. – Abhinav

2

Musisz użyć 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 uruchamia swój kod co 5 sekund

PS: nie zapomnij o unieważnienie stoper kiedy zrobiłeś z tym. Wystarczy zadzwonić pod numer timer.invalidate(). W przeciwnym razie wystąpi awaria.

+0

Okay dzięki. Jestem nowy na szybkie ... jak wyglądałaby funkcja getNextColor? – user3390658

+0

@ user3390658 aktualizacja do pobrania. ale to nie jest najlepsze rozwiązanie :) – Arsen

3

poniższy kod pomaga utrzymać umożliwienia interakcji użytkownika z animowanym widoku. Losowe generowanie kolorów (użyj w razie potrzeby).

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) 
    }