0
如何在此UIColor扩展名中包含alpha?如何扩展此UIColor十六进制函数以包含alpha?
extension UIColor {
convenience init(hex:Int) {
self.init(red:(hex >> 16) & 0xff, green:(hex >> 8) & 0xff, blue:hex & 0xff)
}
}
如何在此UIColor扩展名中包含alpha?如何扩展此UIColor十六进制函数以包含alpha?
extension UIColor {
convenience init(hex:Int) {
self.init(red:(hex >> 16) & 0xff, green:(hex >> 8) & 0xff, blue:hex & 0xff)
}
}
首先,UIColor
没有任何可以取整数的初始值设定项。我假设你有另一个类似于this的扩展。您必须添加对alpha的支持
// we cannot name the param "alpha" because of a name collision
convenience init(red: Int, green: Int, blue: Int, a: Int = 255) {
self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: CGFloat(a)/255)
}
convenience init(hex:Int) {
self.init(red:(hex >> 16) & 0xff, green:(hex >> 8) & 0xff, blue:hex & 0xff, a:(hex >> 24) & 0xff)
}
(假设alpha是第一个组件)。
如果你想阿尔法只是一个附加的十进制数
convenience init(red: Int, green: Int, blue: Int, a: CGFloat = 1.0) {
self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: a)
}
convenience init(hex:Int, a: CGFloat = 1.0) {
self.init(red:(hex >> 16) & 0xff, green:(hex >> 8) & 0xff, blue:hex & 0xff, a: a)
}