How to make a random color with Swift

后端 未结 11 1537
甜味超标
甜味超标 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:24
    func anotherGetRandomColor()->UIColor{
    
        let newRed   = Double(arc4random_uniform(256))/255.0
        let newGreen = Double(arc4random_uniform(256))/255.0
        let newBlue  = Double(arc4random_uniform(256))/255.0
    
        return UIColor(red: CGFloat(newRed), green: CGFloat(newGreen), blue: CGFloat(newBlue), alpha: 1.0)
    }
    
    0 讨论(0)
  • 2020-11-27 04:25

    Make a function to generate random color:

    func getRandomColor() -> UIColor {
         //Generate between 0 to 1
         let red:CGFloat = CGFloat(drand48())   
         let green:CGFloat = CGFloat(drand48()) 
         let blue:CGFloat = CGFloat(drand48())  
    
         return UIColor(red:red, green: green, blue: blue, alpha: 1.0)
    }
    

    Now, you can call this function whenever you need random color.

    self.view.backgroundColor = getRandomColor()
    
    0 讨论(0)
  • 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
    

    0 讨论(0)
  • 2020-11-27 04:30

    Swift 5.1

    Make This function and generate Random color.

    e.g. view.backgroundColor = random()

    func random() -> UIColor {
            return UIColor(red: .random(in: 0...1),
                           green: .random(in: 0...1),
                           blue: .random(in: 0...1),
                           alpha: 1.0)
        }
    
    0 讨论(0)
  • 2020-11-27 04:33

    With Swift 4.2, you can simplify this by using the new random functions which have been added:

    extension UIColor {
      static func random () -> UIColor {
        return UIColor(
          red: CGFloat.random(in: 0...1),
          green: CGFloat.random(in: 0...1),
          blue: CGFloat.random(in: 0...1),
          alpha: 1.0)
      }
    }
    

    There are more details here.

    0 讨论(0)
  • 2020-11-27 04:36

    For Swift 4.2

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

    For Swift 3 and above:

    extension CGFloat {
        static var random: CGFloat {
            return CGFloat(arc4random()) / CGFloat(UInt32.max)
        }
    }
    
    extension UIColor {
        static var random: UIColor {
            return UIColor(red: .random, green: .random, blue: .random, alpha: 1.0)
        }
    }
    

    Usage:

    let myColor: UIColor = .random
    
    0 讨论(0)
提交回复
热议问题