Created
December 14, 2015 08:42
-
-
Save lynxerzhang/56ac46fd28f0045cf819 to your computer and use it in GitHub Desktop.
Random Color in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Jared Davidson's Random Colorization | |
//run in xcode7.1 and swift2.1 | |
//drand48返回0 - 1的随机Double数值 | |
var red = CGFloat(drand48()) | |
var green = CGFloat(drand48()) | |
var blue = CGFloat(drand48()) | |
var view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) | |
//随机色 | |
view.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0) | |
//根据对应的red,green,blue数值返回UIColor对象(0 - 255) | |
func rgbColor(red: Int, _ green: Int, _ blue: Int) -> UIColor | |
{ | |
return UIColor(red: CGFloat(red) / 0xFF, green: CGFloat(green) / 0xFF, blue: CGFloat(blue) / 0xFF, alpha: 1.0) | |
} | |
view.backgroundColor = rgbColor(255, 255, 0) //纯黄色的背景 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment