使用默认颜色启动应用

时间:2017-09-25 13:31:05

标签: ios swift xcode uicolor

menuView必须具有每个人的默认颜色。但是,使用RGB滑块的应用程序设置中的任何人都可以更改menuView的颜色。 所以我想用黄色设置默认颜色,并设置滑块以更改颜色menuView。颜色变化后,可以保存。

import UIKit

class ViewController: UIViewController {

  let defaults = UserDefaults.standard

  @IBOutlet weak var tableView: UITableView!
  @IBOutlet weak var menuView: UIViewX!

  var redval = Float()
  var greenval = Float()
  var blueval = Float()


  @IBOutlet weak var NewMessage: UIButton!
  @IBOutlet weak var Camera: UIButton!
  @IBOutlet weak var NewPost: UIButton!

  var tableData: [Model] = []

  override func viewDidLoad() {
    super.viewDidLoad()



    self.menuView.backgroundColor = UIColor(red: CGFloat(redval), green: CGFloat(greenval), blue: CGFloat(blueval), alpha: 0.45)
  }
 }

使用字符串

self.menuView.backgroundColor = UIColor(red: CGFloat(redval), green: 
CGFloat(greenval), blue: CGFloat(blueval), alpha: 0.45)

我可以通过滑块RGB使用app更改颜色,但现在我想首先将默认颜色添加到可以更改的位置。

更改颜色的代码滑块

import UIKit

 class ViewSettings: UIViewController {

@IBOutlet var redslider: UISlider!
@IBOutlet var greenslider: UISlider!
@IBOutlet var blueslider: UISlider!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func change(_ sender: AnyObject) {
    self.performSegue(withIdentifier: "ChangeColor", sender: self)
}

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ChangeColor" {
        let destanitaion = segue.destination as! ViewController
        destanitaion.redval = redslider.value
        destanitaion.blueval = blueslider.value
        destanitaion.greenval = greenslider.value
    }
}


}

2 个答案:

答案 0 :(得分:2)

将每个颜色分量存储为Double中的UserDefaults

// when saving

defaults.set(redVal, forKey: "defaultColorRedValue")
defaults.set(greenVal, forKey: "defaultColorGreenValue")
defaults.set(blueVal, forKey: "defaultColorBlueValue")

提取每个值:

override func viewDidLoad() {
    super.viewDidLoad()

    redVal = CGFloat(defaults.double(forKey: "defaultColorRedValue"))
    greenVal = CGFloat(defaults.double(forKey: "defaultColorGreenValue"))
    blueVal = CGFloat(defaults.double(forKey: "defaultColorBlueValue"))

    self.menuView.backgroundColor = UIColor(red: redval, green: greenval, blue: blueval, alpha: 0.45)
}

答案 1 :(得分:0)

- 应用程序终止后设置RGB默认值的用户默认值

滑块上的

更改存储RGB

let defaults = UserDefaults.standard
defaults.set(redval, forKey: “Red”)
defaults.set(greenval, forKey: “green”)
defaults.set(blueval, forKey: “blue”)

在视图上,从用户默认值

加载获取值
let red = defaults.float(forKey: "Red")
let green = defaults.float(forKey: "green")
let blue = defaults.float(forKey: "blue")

 self.menuView.backgroundColor = UIColor(red: CGFloat(red), green:CGFloat(green), blue: CGFloat(blue), alpha: 0.45)