How to make a random color with Swift

后端 未结 11 1541
甜味超标
甜味超标 2020-11-27 04:01

How I can make a random color function using Swift?

import UIKit

class ViewController: UIViewController {

    var randomNumber = arc4random_uniform(20)
            


        
11条回答
  •  感情败类
    2020-11-27 04:26

    For random solid colors you can use UIColor HSB initializer and randomize only the hue:

    extension UIColor {
        static var random: UIColor {
            return .init(hue: .random(in: 0...1), saturation: 1, brightness: 1, alpha: 1)
        }
    }
    

    let color1: UIColor = .random
    let color2: UIColor = .random
    let color3: UIColor = .random
    let color4: UIColor = .random
    let color5: UIColor = .random
    

提交回复
热议问题