【问题标题】:How to select a random color in Swift如何在 Swift 中选择随机颜色
【发布时间】:2019-03-29 23:58:37
【问题描述】:

我有一小段代码可以选择一些颜色,我想知道如果我想在代码下选择这些颜色之间的随机颜色怎么办?

extension UIColor {
    static var random: UIColor {
        return UIColor(red: .random(in: 0...2), green: .random(in: 0...2), blue: .random(in: 0...2), alpha: 1.0)
    }
}

我想在我的扩展程序中选择的颜色

【问题讨论】:

  • 如果您只想使用以上 5 种颜色,则在数组中添加颜色代码并从十六进制字符串中加载颜色。

标签: ios xcode colors


【解决方案1】:

要从UIColors 列表中获取随机颜色,只需在UIColor 元素的collection 上调用randomElement(),即

extension UIColor {
    static func random(from colors: [UIColor]) -> UIColor? {
        return colors.randomElement()
    }
}

你可以像这样使用它:

let randomColor = UIColor.random(from: [.red, .yellow, .green, .blue, .purple])

我在这里使用了示例颜色。使用您自己想要使用的特定颜色。

如果您有 hex 值形式的颜色,则需要在扩展程序中添加另一个方法将该 hex 值转换为 UIColor 实例。

如果有任何问题,请告诉我。

【讨论】:

    【解决方案2】:

    如果您只想使用您在问题中添加的颜色,请在Array 中添加所需的颜色十六进制字符串。

    let arrColors = ["ff0000", "228B22", "FFFF00", "800080", "ffa500"] // Use your color hex
    

    现在你需要像这样从上面的数组中选择随机对象。

    let randomIndex = Int(arc4random_uniform(UInt32(arrColors.count)))
    self.vwNavigationBar.backgroundColor = self.hexStringToUIColor(hex: arrColors[randomIndex])
    

    将十六进制字符串转换为颜色的函数

    func hexStringToUIColor (hex:String) -> UIColor {
        var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
    
        if (cString.hasPrefix("#")) {
            cString.remove(at: cString.startIndex)
        }
    
        if ((cString.count) != 6) {
            return UIColor.gray
        }
    
        var rgbValue:UInt32 = 0
        Scanner(string: cString).scanHexInt32(&rgbValue)
    
        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }
    

    【讨论】:

      【解决方案3】:

      试试这个。它应该可以工作。

       extension UIColor {
      
           static func random() -> UIColor {
              return UIColor.rgb(CGFloat.random(in: 0..<256), green: CGFloat.random(in: 0..<256), blue: CGFloat.random(in: 0..<256))
           }
      
           static func rgb(_ red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
            return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
           }
      
           convenience init(red: Int, green: Int, blue: Int) {
              assert(red >= 0 && red <= 255, "Invalid red component")
              assert(green >= 0 && green <= 255, "Invalid green component")
              assert(blue >= 0 && blue <= 255, "Invalid blue component")
      
              self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
           }
      
           convenience init(rgb: Int) {
              self.init(
                red: (rgb >> 16) & 0xFF,
                green: (rgb >> 8) & 0xFF,
                blue: rgb & 0xFF
             )
            }
      
          static func getColor() -> UIColor {
             let chooseFrom = [0x064545,0x708545,0x45580] //Add your colors here
             return UIColor.init(rgb: chooseFrom[Int.random(in: 0..<chooseFrom.count)])
           }
      
         }
      

      【讨论】:

      • 在随机函数中,它显示错误:无法将'Int'类型的值转换为预期的参数类型'CGFloat'
      • 使用 CGFloat 而不是 Int 作为随机值。它会工作
      • 使用 UIColor.rgb() 时出现错误:在 void 函数中出现意外的非 void 返回值
      • 检查一下,我忘了从 random() 返回。现在它应该可以工作了
      • 我不太明白应该在哪里插入 return ?
      猜你喜欢
      • 2012-08-24
      • 2019-10-29
      • 2021-09-29
      • 2014-10-07
      • 2014-06-12
      • 1970-01-01
      • 2011-07-07
      • 2017-03-29
      • 2014-12-26
      相关资源
      最近更新 更多