2015-10-17 18 views
6

Tôi muốn màu nền của ứng dụng iOS thay đổi giữa bốn màu trên x số giâyHình động màu nền nhanh chóng

Đây là những gì tôi có cho đến nay (chính xác những gì tôi muốn khi tôi chỉ định 2 màu sắc)

Tôi cũng cần hoạt ảnh để chạy trong vòng lặp vô hạn.

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

Hãy thử giải pháp của tôi. Sử dụng khối hoàn thành hoạt ảnh luôn tốt hơn chạy hẹn giờ. – Abhinav

Trả lời

12

Hãy thử điều này:

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

Trong trường hợp bạn muốn có một hình ảnh động lặp đi lặp lại liên tục, hãy thử này ra:

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 đó là tuyệt vời! – user3390658

+0

Vấn đề duy nhất với giải pháp thứ hai là nó bỏ qua hai màu đầu tiên, nó chỉ chuyển đổi giữa màu xám và màu đỏ – user3390658

+0

Quá trình chuyển đổi quá nhanh để thấy sự thay đổi thực sự. Bạn có thể sử dụng khối hoàn thành và chơi với thời gian để phù hợp với nhu cầu của bạn nếu bạn muốn đi với tùy chọn 2. – Abhinav

2

Bạn phải sử dụng 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 chạy mã của bạn mỗi 5 giây

PS: đừng quên hẹn giờ làm mất hiệu lực khi bạn thực hiện với nó. Chỉ cần gọi timer.invalidate() cho nó. Nếu không, bạn sẽ gặp sự cố.

+0

Cảm ơn rất tuyệt. Tôi mới đến nhanh chóng .. một hàm getNextColor trông như thế nào? – user3390658

+0

@ user3390658 kiểm tra cập nhật. nhưng nó không phải là giải pháp tốt nhất :) – Arsen

3

Dưới đây đang giúp giữ cho phép người dùng tương tác với xem hoạt hình. Tạo màu ngẫu nhiên (sử dụng trong trường hợp cần thiết).

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) 
    } 
Các vấn đề liên quan