Код: Выделить всё
UIColorExtension.swiftКод: Выделить всё
import UIKit
extension UIColor {
static var random: UIColor {
// Seed (only once)
srand48(Int(arc4random()))
return UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0)
}
}
Код: Выделить всё
ViewController.SwiftКод: Выделить всё
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// apply random color on view did load
applyRandomColor()
}
@IBAction func btnHandler(_ sender: Any) {
// on each button click, apply random color to view
applyRandomColor()
}
func applyRandomColor() {
view.backgroundColor = UIColor.random
}
}
Подробнее здесь: https://stackoverflow.com/questions/494 ... e-in-swift
Мобильная версия