swift - 为什么只有内置的 UIColors 在这里工作?

标签 swift scenekit uicolor scnnode

<分区>

在进一步尝试解决 this question 时惨遭失败就我自己而言,我正在尝试一些我认为肯定有效的方法:

func switchColor(data:UInt32){
        switch data {
        case 1..<200:
            backgroundGeometry.firstMaterial?.diffuse.contents =
                UIColor(red: CGFloat(242), green: CGFloat(90), blue: CGFloat(90), alpha: 1.0)
        case 200..<400:
            backgroundGeometry.firstMaterial?.diffuse.contents =
                UIColor(red: CGFloat(252), green: CGFloat(162), blue: CGFloat(115), alpha: 1.0)
        case 400..<600:
            backgroundGeometry.firstMaterial?.diffuse.contents =
                UIColor(red: CGFloat(244), green: CGFloat(235), blue: CGFloat(99), alpha: 1.0)
        case 600..<800:
            backgroundGeometry.firstMaterial?.diffuse.contents =
                UIColor(red: CGFloat(110), green: CGFloat(195), blue: CGFloat(175), alpha: 1.0)
        case 800..<1000:
            backgroundGeometry.firstMaterial?.diffuse.contents =
                UIColor(red: CGFloat(91), green: CGFloat(118), blue: CGFloat(211), alpha: 1.0)
        default:
            backgroundGeometry.firstMaterial?.diffuse.contents = UIColor.green
        }
    }

所有非默认情况都将节点变为白色。 默认情况确实将其变为绿色 - 在每种情况下,UIColor.red、UIColor.blue 等语句也可以正常工作。

那么为什么上面的语句不起作用呢?

希望你能帮忙,我在这里完全不知所措:(

编辑:感谢您 swift 而正确的回答!所有人都接受并投票,但我太新手了,无法展示。谢谢! :)

最佳答案

这应该适合你:

func switchColor(data: UInt32) {
    guard let contents = backgroundGeometry.firstMaterial?.diffuse.contents else {
        fatalError("First material is nil") // If this really can be empty, just replace this with return
    }

    switch data {
    case 1..<200:
        contents = UIColor(red: 242/255, green: 90/255, blue: 90/255, alpha: 1)
    case 200..<400:
        contents = UIColor(red: 252/255, green: 162/255, blue: 115/255, alpha: 1)
    case 400..<600:
        contents = UIColor(red: 244/255, green: 235/255, blue: 99/255, alpha: 1)
    case 600..<800:
        contents = UIColor(red: 110/255, green: 195/255, blue: 175/255, alpha: 1)
    case 800..<1000:
        contents = UIColor(red: 91/255, green: 118/255, blue: 211/255, alpha: 1)
    default:
        contents = .green
    }
}

颜色的最大值是1.0,不是255,所以需要对值进行除法。

关于swift - 为什么只有内置的 UIColors 在这里工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54960187/

相关文章:

swift - 快速相邻的颜色

ios - 遍历数组并更改每个 UITableView Cell 的背景颜色

ios - 如何在 Swift 中向 UIImageView 添加自定义形状?

Swift 任务延续误用;函数多次尝试恢复其继续

swift - 如何修复 'dynamic' 属性 'IpfSetId' 也必须是 '@objc' ?

ios - ARKit + SceneKit : Using reconstructed scene for physics?

swift - 如何在 Swift 的惰性初始化中使用 self

ios - 在创建游戏的范围之外,如何在 iOS 中创建程序生成的可视化效果?

swift - 如何在将场景保存/导出为 .dae 格式时保留 SceneKit 中纹理的平铺信息(contentTransform 属性)?

ios - 在 UIColor 子类中设置属性时崩溃