如何在swift中定义HEX颜色宏

时间:2015-09-01 10:08:25

标签: ios objective-c swift

我正在学习swift现在将#define转换为swift

时遇到一些麻烦
#define HColorToRGB(rgbValue,alphaValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:alphaValue]

如何将Obj-C定义转换为Swift代码?

感谢阅读干杯

1 个答案:

答案 0 :(得分:2)

func HColorToRGB(rgbValue: Int, alpha: Float) -> UIColor {
return UIColor(
    red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
    green: CGFloat((rgbValue & 0xFF00) >> 8) / 255.0,
    blue: CGFloat(rgbValue &0xFF) / 255.0,
    alpha: CGFloat(alpha)
)
}


yourView.backgroundColor = HColorToRGB("passyourValue","alphaValue")