UIColor 扩展便利初始化不起作用
UIColor extension convenience init not working
convenience init(red:Int,green:Int,blue:Int,alpha:CGFloat) {
var red: CGFloat = CGFloat(red)/255.0
var green: CGFloat = CGFloat(green)/255.0
var blue: CGFloat = CGFloat(blue)/255.0
self.init(red:red, green:green, blue:blue, alpha:alpha)
}
我编写上面的代码是为了提供一种更方便的方式来声明我的自定义 uicolor。但不知何故,它通过调用自身直到堆栈溢出使我的应用程序崩溃。这里有什么问题?
另外,我刚刚意识到我没有明确调用这个 init 函数。但是当这个错误发生时我正在调用 UIColor.whiteColor()
。当然,如果我显式调用这个函数,还是会报错!
我认为这样做没有任何不便:
let myCustomColorHSBa = UIColor(hue: 120/360, saturation: 0.25 , brightness: 1.0 , alpha: 1)
let myCustomColorRGBa = UIColor(red: 191/255, green: 1, blue: 191/255, alpha: 1)
但如果你真的需要一个,你可以这样做:
extension UIColor {
convenience init(red: Int = 0, green: Int = 0, blue: Int = 0, opacity: Int = 255) {
precondition(0...255 ~= red &&
0...255 ~= green &&
0...255 ~= blue &&
0...255 ~= opacity, "input range is out of range 0...255")
self.init(red: CGFloat(red)/255, green: CGFloat(green)/255, blue: CGFloat(blue)/255, alpha: CGFloat(opacity)/255)
}
}
UIColor(red: 255) // r 1.0 g 0.0 b 0.0 a 1.0 (Red)
UIColor(red: 255, green: 255) // r 1.0 g 1.0 b 0.0 a 1.0 (Yellow)
UIColor(red: 255, blue: 255) // r 1.0 g 0.0 b 1.0 a 1.0 (Magenta)
UIColor(green: 255) // r 0.0 g 1.0 b 0.0 a 1.0 (Green)
UIColor(green: 255, blue: 255) // r 0.0 g 1.0 b 1.0 a 1.0 (Cyan)
UIColor(blue: 255) // r 0.0 g 0.0 b 1.0 a 1.0 (Blue)
UIColor(red: 255, green: 192, blue: 203) // r 1.0 g 0.753 b 0.796 a 1.0 (Pink)
UIColor(red: 255, green: 215) // r 1.0 g 0.843 b 0.0 a 1.0 (Gold)
另一种方法是这样做 (swift 3)
extension UIColor {
convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a:CGFloat) {
self.init(red: r/255, green: g/255, blue: b/255, alpha: a/255)
}
}
let myColor = UIColor(r:255, g:255, b:255, a:255)
convenience init(red:Int,green:Int,blue:Int,alpha:CGFloat) {
var red: CGFloat = CGFloat(red)/255.0
var green: CGFloat = CGFloat(green)/255.0
var blue: CGFloat = CGFloat(blue)/255.0
self.init(red:red, green:green, blue:blue, alpha:alpha)
}
我编写上面的代码是为了提供一种更方便的方式来声明我的自定义 uicolor。但不知何故,它通过调用自身直到堆栈溢出使我的应用程序崩溃。这里有什么问题?
另外,我刚刚意识到我没有明确调用这个 init 函数。但是当这个错误发生时我正在调用 UIColor.whiteColor()
。当然,如果我显式调用这个函数,还是会报错!
我认为这样做没有任何不便:
let myCustomColorHSBa = UIColor(hue: 120/360, saturation: 0.25 , brightness: 1.0 , alpha: 1)
let myCustomColorRGBa = UIColor(red: 191/255, green: 1, blue: 191/255, alpha: 1)
但如果你真的需要一个,你可以这样做:
extension UIColor {
convenience init(red: Int = 0, green: Int = 0, blue: Int = 0, opacity: Int = 255) {
precondition(0...255 ~= red &&
0...255 ~= green &&
0...255 ~= blue &&
0...255 ~= opacity, "input range is out of range 0...255")
self.init(red: CGFloat(red)/255, green: CGFloat(green)/255, blue: CGFloat(blue)/255, alpha: CGFloat(opacity)/255)
}
}
UIColor(red: 255) // r 1.0 g 0.0 b 0.0 a 1.0 (Red)
UIColor(red: 255, green: 255) // r 1.0 g 1.0 b 0.0 a 1.0 (Yellow)
UIColor(red: 255, blue: 255) // r 1.0 g 0.0 b 1.0 a 1.0 (Magenta)
UIColor(green: 255) // r 0.0 g 1.0 b 0.0 a 1.0 (Green)
UIColor(green: 255, blue: 255) // r 0.0 g 1.0 b 1.0 a 1.0 (Cyan)
UIColor(blue: 255) // r 0.0 g 0.0 b 1.0 a 1.0 (Blue)
UIColor(red: 255, green: 192, blue: 203) // r 1.0 g 0.753 b 0.796 a 1.0 (Pink)
UIColor(red: 255, green: 215) // r 1.0 g 0.843 b 0.0 a 1.0 (Gold)
另一种方法是这样做 (swift 3)
extension UIColor {
convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a:CGFloat) {
self.init(red: r/255, green: g/255, blue: b/255, alpha: a/255)
}
}
let myColor = UIColor(r:255, g:255, b:255, a:255)