[Swift] Random color

Hello, this is @ Zhalen. I'm really going to die from lack of money.

In this article, I will introduce an extension that outputs colors randomly like this.

ezgif.com-resize.gif

Extension

This is the extension.


//MARK:-Random color
extension UIColor {
    static func random() -> UIColor {
        let red: CGFloat = CGFloat.random(in: 0...1)
        let green: CGFloat = CGFloat.random(in: 0...1)
        let blue: CGFloat = CGFloat.random(in: 0...1)
        
        return UIColor.init(red: red, green: green, blue: blue, alpha: 1)
    }
}

Please describe this ↑ somewhere in the file

UIColor.random()

If you write, a random color will appear.

here,


CGFloat.random(in: 0...1)

Is a `that randomly returns a CGFloat type from` 0 to 1, so this extension is realized by generating a random rgb value with a random initial value of `` `UIColor```. I am.

It's a promotion. Please install it!

スクリーンショット 2020-12-30 17.42.50.png

Just the other day, we released a questionnaire-specific SNS " Vote. "!

This app is useful not only when you are at a loss or worried about your choice, but also in practical aspects such as corporate A/B testing and test marketing! The design is very sophisticated, and anyone can easily take questionnaires and votes.

Click here to install: https://apps.apple.com/us/app/vote/id1542436046

Recommended Posts

[Swift] Random color
[swift5] How to specify color in hexadecimal
[Swift] Closure
[Swift] Change the color of SCN Node