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)
}