Search code examples
swiftuislider

How to Change Opacity of Selected background color of View using UISlider?


I want to create custom colour picker according to my requirement. I do not want to go with third party. How can I increase or decrease the opacity of particular when slider value is changed?

screenshot is below:

Slider demo in xcode


Solution

  • After you set up your slider and create a connection to the value change like so....

    enter image description here

    Then change the minimum and maximum values of the slider to 0 to 1

    then implement this inside your sliderValueChanged handler

    @IBAction func sliderValueChanged(sender: UISlider) {
         var currentValue = Int(sender.value)
    
         self.view.backgroundColor = UIColor(white: 1, alpha: sender.value)
    }