如何确定View的背景颜色?

时间:2017-10-30 22:58:20

标签: ios swift

我试图获取背景的颜色值。我的应用程序的背景颜色设置为按下按钮随机,但我希望能够在后台获取该颜色的值并确定它是什么颜色。这是我从研究中得到的,但每当我将结果打印到控制台时,我就得到0。任何帮助真的很感激!

func getBackgroundColor(){
    let color = self.view.backgroundColor

    let colour = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)

    self.view.backgroundColor = color

    var red = CGFloat()
    var green = CGFloat()
    var blue = CGFloat()
    var alpha = CGFloat()

    print(red, green, blue, alpha)

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

}

1 个答案:

答案 0 :(得分:0)

 let red = self.view.backgroundColor?.ciColor.red
        let blue = self.view.backgroundColor?.ciColor.blue
        let green = self.view.backgroundColor?.ciColor.green
        let alpha = self.view.backgroundColor?.ciColor.alpha

        print("r \(red) b \(blue) g \(green) a \(alpha) ")