本文介绍了动画UIView背景颜色迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用随机颜色动画UIView的背景。
这是我到目前为止所做的:
I'd like to animate background of UIView with random colours.here's what i've done so far:
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var colours = UIColor()
override func viewDidLoad() {
super.viewDidLoad()
getRandomColor()
timerF()
UIView.animateWithDuration(2, delay: 0.0, options:[UIViewAnimationOptions.Repeat, UIViewAnimationOptions.Autoreverse], animations: {
self.view.backgroundColor = self.colours
}, completion:nil)
}
func timerF(){
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("getRandomColor"), userInfo: nil, repeats: true)
}
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背景看起来像一个随机的彩虹。
But it just generates a random colour at the start and use this single colour in my animation. I'd like to find a way to animate colours, so UIVIew background would look like a random rainbow.
推荐答案
动画 getRandomColor
。
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)
UIView.animate(withDuration: 1.0, delay: 0.0, options:[.repeat, .autoreverse], animations: {
self.view.backgroundColor = UIColor(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)
}, completion:nil)
}
这篇关于动画UIView背景颜色迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!