在Swift中比较UIColors的问题

时间:2015-06-05 23:51:29

标签: ios swift sprite-kit uicolor

我需要比较两个UIColors,但由于某种原因它总是返回false。我尝试使用==.isEqual()进行比较,但它们似乎都不起作用。

//This is a sample of the colors I have created
let blue_color = UIColor(red: 122/255, green: 180/255, blue: 190/255, alpha: 1)


//This is the SpriteNode I have to compare

let square = SKSpriteNode(color: randomColorController(), size: ksquaresize)

randomColorController()只是一个随机化颜色并返回它的函数,因此在创建square时会调用它。

func randomColorController() -> UIColor {
    let random = arc4random_uniform(3) + 1

    switch random {
    case 1:
        let color = blue_color
        return color
    case 2:
        let color = yellow_color
        return color
    case 3:
        let color = yellow_color
        return color
    default:
        let color = UIColor.clearColor()
        return color
    }

然后,根据我创建的方块的位置,它将检查碰撞,比较我创建的方块的颜色和我在开始时初始化的颜色。

func checkCollision(currentTime: CFTimeInterval, Square: SKSpriteNode) -> Int{
    let color = Square.color

    print(color.isEqual(blue_color))
    print(color.isEqual(red_color))
    print(color.isEqual(yellow_color))

    if Square.position.y >= 0 && Square.position.y <= 40 {
        if color.isEqual(blue_color) && (Square.position.x < basesize.width) {
                // ADDS 1 POINT TO THE SCORE LABEL
                flag = 1
                points += 1
            } else if color.isEqual(red_color) && (Square.position.x > (basesize.width*2)){
                flag = 1
                points += 1
            } else if color.isEqual(yellow_color) && (Square.position.x < (basesize.width*2)) &&  (Square.position.x > basesize.width) {
                flag = 1
                points += 1
            } else {
                flag = -1
            }
    }

color.isEqual(blue_color)或任何其他颜色似乎不起作用。我已打印Square.colorblue_color(和其他人),但它们相符。但它总会返回false

2 个答案:

答案 0 :(得分:5)

您可以使用&#34; ==&#34;或等于。我刚刚测试了它们,它们工作正常:

let redColor = UIColor.redColor()
let greenColor = UIColor.greenColor()
let blueColor = UIColor.blueColor()

let testColor = UIColor.greenColor()


println( testColor == redColor )    //  false
println( testColor == greenColor )  // true
println( testColor == blueColor )   // false

println( testColor.isEqual(redColor) )    //  false
println( testColor.isEqual(greenColor) )  // true
println( testColor.isEqual(blueColor) )   // false

我刚刚重现了这个问题,只有在提取SKSpriteNode颜色后才会发生,正如你所说的只有分数颜色。您可以解决此问题,比较颜色描述如下:

let blue_color = UIColor(red: 122/255, green: 180/255, blue: 190/255, alpha: 1)
let yellow_color = UIColor(red: 253/255, green: 213/255, blue: 123/255, alpha: 1)
let red_color = UIColor(red: 238/255, green: 116/255, blue: 71/255, alpha: 1)

func randomColorController() -> UIColor {
    let random = arc4random_uniform(3) + 1

    switch random {
    case 1:
        return blue_color
    case 2:
        return red_color
    case 3:
        return yellow_color
    default:
        return UIColor.clearColor()
    }
}



let square = SKSpriteNode(color: randomColorController(), size: CGSize(width: 30, height: 30))

if square.color.description ==  blue_color.description {
    println(true)
}
if square.color.description == red_color.description {
    println(true)
}
if square.color.description == yellow_color.description {
    println(true)
}

答案 1 :(得分:4)

extension UIColor {
    func isEqual(color: UIColor?) -> Bool {
        guard let color = color else { return false }

        var red:CGFloat   = 0
        var green:CGFloat = 0
        var blue:CGFloat  = 0
        var alpha:CGFloat = 0
        self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

        var targetRed:CGFloat   = 0
        var targetGreen:CGFloat = 0
        var targetBlue:CGFloat  = 0
        var targetAlpha:CGFloat = 0
        color.getRed(&targetRed, green: &targetGreen, blue: &targetBlue, alpha: &targetAlpha)

        return (Int(red*255.0) == Int(targetRed*255.0) && Int(green*255.0) == Int(targetGreen*255.0) && Int(blue*255.0) == Int(targetBlue*255.0) && alpha == targetAlpha)
    }
}

我在swift 3,4中进行了测试