getRed()如何在iOS 9 Swift中的UIColor对象中正常工作

时间:2015-10-23 10:22:10

标签: ios swift uicolor

有关此代码如何在iOS 9中使用xcode 7的任何建议?

color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

它说"预期声明"

以下是代码:

var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var alpha: CGFloat = 0.0

color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
webLabel.text = String(format: "#%02X%02%02X", CInt(red*255),CInt(green*255),CInt(blue*255))

"颜色"以前是这样分配的:

var color: UIColor
{
    return UIColor(hue: CGFloat(hue/360), saturation: CGFloat(saturation/100), brightness: CGFloat(brightness/100), alpha: 1.0)
}

我刚刚开始使用xcode 7和swift,所以我是新手。提前谢谢。

1 个答案:

答案 0 :(得分:3)

SWIFT 2.0整体解决方案

下面是您的问题的总解决方案/ Anwser。如果有效,请将此答案视为正确。

func converUIColorToHex(colorToConver:UIColor)-> String{

    var red:CGFloat = 0
    var green:CGFloat = 0
    var blue:CGFloat = 0
    var alpha:CGFloat = 0

    colorToConver.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

    print(red)

    if(red < 0) {red = 0}

    if(green < 0) {green = 0}

    if(blue < 0) {blue = 0}

    if(red > 255) {red = 255}

    if(green > 255) {green = 255}

    if(blue > 255) {blue = 255}

    print(red)

    let decimalCode = Int((red * 65536) + (green * 256) + blue)

    let hexColorCode = String(decimalCode, radix: 16)

    print(hexColorCode)

    return hexColorCode
}