Joh*_*ite 16 animation colors uiview ios swift
我想用随机颜色动画UIView的背景.这是我到目前为止所做的:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
但它只是在开始时生成一个随机颜色,并在我的动画中使用这种单一颜色.我想找到一种动画颜色的方法,所以UIVIew背景看起来像一个随机的彩虹.
Cal*_*leb 38
把动画放进去吧getRandomColor.
func getRandomColor() {
let red = CGFloat((arc4random() % 256)) / 255.0
let green = CGFloat((arc4random() % 256)) / 255.0
let blue = CGFloat((arc4random() % 256)) / 255.0
let alpha = CGFloat(1.0)
UIView.animate(withDuration: 1.0, delay: 0.0, options:[.repeat, .autoreverse], animations: {
self.view.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)
}, completion:nil)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16313 次 |
| 最近记录: |