【问题标题】:Trying to use Slider to adjust color in Swift / iOS 8 - get "Extra Argument Saturation In Call" error尝试使用 Slider 在 Swift / iOS 8 中调整颜色 - 得到“Extra Argument Saturation In Call”错误
【发布时间】:2015-01-25 11:09:16
【问题描述】:

我正在学习使用 Swift for iOS 中的控件。我正在尝试构建一个简单的应用程序,该应用程序使用 Slider 使用 UIColor 类调整屏幕的背景颜色。

// function is called when slider is moved.
@IBAction func sliderValueChanged(sender: UISlider) {

    // creates variable to hold new color
    var newBackgroundColor : UIColor

    // creates variable holding the value from slider
    var sliderValue = slider.value

    // changes the newBackgroundColor variable to new color values. 
    newBackgroundColor = UIColor(hue: sliderValue, saturation: 0.5, brightness: 0.5, alpha: 0.5)

    // changes the background color
    self.view.backgroundColor = newBackgroundColor

滑块似乎连接正确,调整背景的代码有效。我创建了一个变量来保存滑块的值(介于 0.0 和 1.0 之间),但是当我尝试将此变量插入 UIColor 代码时,它会出现错误“Extra Argument Saturation In Call”。

PS:对不起,如果代码是杂乱无章的。这是我的第一篇文章(我还不能发布图片)。

【问题讨论】:

    标签: ios xcode swift


    【解决方案1】:

    因为sliderValue 的类型为Float,而hue 的类型为CGFloat。你可以这样投射。

    let sliderValue = CGFloat(slider.value)
    

    【讨论】:

    • 非常感谢。这样可行。我还发现了参考文件中列为 CGFloat 的 Hue 类型,并将在未来寻找它。感谢您帮助我今天学到了一些东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2015-07-13
    • 2013-09-28
    • 2014-11-27
    • 2015-01-13
    • 2015-04-18
    • 2021-12-28
    相关资源
    最近更新 更多