任何一款app开发都会有各种颜色的配置,下面谈一下swift的颜色拓展类,不管UI给你什么格式的颜色都可以得心应手!
首先知道几个缩写意思:R:red(红色), G:green(绿色), B:blue(蓝色) ,A:alpha(透明度)
颜色扩展类(名字自己起)
extension UIColor {
//16进制转rgba(透明度自己定)
class func RgbaColor(rgb:Int, alpha: CGFloat) ->UIColor {
return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16))/255.0,
green: ((CGFloat)((rgb & 0xFF00) >> 8))/255.0,
blue: ((CGFloat)(rgb & 0xFF))/255.0,
alpha: alpha)
}
//16进制转rgb(透明度数值为固定值1.0)
class func RgbColor(rgb:Int) -> UIColor {
return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16))/255.0,
green: ((CGFloat)((rgb & 0xFF00) >> 8))/255.0,
blue: ((CGFloat)(rgb & 0xFF))/255.0,
alpha: 1.0)
}
//随机颜色(给控件一个背景色,方便调试)
class func RandomColors() -> UIColor {
let red = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
let green = CGFloat( arc4random_uniform(255))/CGFloat(255.0)
let blue = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
let alpha = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
return UIColor.init(red:red, green:green, blue:blue , alpha: alpha)
}
//rgb三数值(传入UI给的三个数值和透明度即可)
class func MyColor(color: CGFloat...) -> UIColor {
return UIColor(red: color[0]/255.0, green: color[1]/255.0, blue: color[2]/255.0, alpha: color[3])
}
class func appColor72() -> UIColor {
return create(72,72,72,1.0)
}
其他页面调用
import UIKit
class ViewController: UIViewController,SecondDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.RgbaColor(rgb:0xFFB6C1, alpha: 0.9)
self.view.backgroundColor = UIColor.RandomColors()
self.view.backgroundColor = UIColor.appColor72()
}
}
这样,不管UI给什么颜色,你都可以得心应手!
网友评论