2017-09-25 77 views
-1

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與應用改變顏色,但現在我想先關稅默認的顏色,這可能是變化的。

代碼滑塊變色

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 
    } 
} 


} 
+1

直截了當地做到這一點是每一個紅,綠,藍值保存爲'UserDefaults'中的'Double'。然後提取每個組件並重建「UIColor」。你也可以創建一個'UserDefaults'擴展名來使這個更清潔。 – Paolo

+0

你能舉個例子嗎?我想要的默認顏色是#colorLiteral(紅色:0.9966391622,綠色:1,藍色:0.2095825737,alpha:0.7946946448)/ *#f2f0f0 */@Paolo –

+0

UP @Paolo查看更新 –

回答

2

存儲各顏色成分作爲DoubleUserDefaults

// 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) 
} 
0

- 用戶的用戶默認爲設置應用程序終止後的RGB默認值,

你的滑塊價值變動店

RGB

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

上查看沒有從用戶的默認加​​載的get值

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) 
相關問題