本文介绍了getRed() 如何在 iOS 9 Swift 中的 UIColor 对象中正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这段代码如何在带有 xcode 7 的 iOS 9 中工作的任何建议?

Any suggestions how this code works in iOS 9 with 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))

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

"color" is assigned previously like so:

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

我刚刚开始使用 xcode 7 并且很快,所以我是新手.提前致谢.

I have just started with the xcode 7 and swift so I am rookie at this. Thanks in advance.

推荐答案

SWIFT 2.0整体解决方案

以下是您问题的完整解决方案/Anwser.如果它有效,请善待这个答案.

below is the total solution/Anwser for your question.Please be kind enough to make this answer as correct if it works.

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
}

这篇关于getRed() 如何在 iOS 9 Swift 中的 UIColor 对象中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:19