简体   繁体   中英

Reset UISlider Thumb to Initial Position

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var squareButton: UIButton!

    var red = CGFloat(255)
    var green = CGFloat(255)
    var blue = CGFloat(255)
    var alpha = CGFloat(1)

    override func viewDidLoad() {
        super.viewDidLoad()
        squareButton.layer.cornerRadius = 100
    }

    @IBAction func sliderR(sender: UISlider) {
        red = CGFloat(lroundf(sender.value))
        squareButton.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha/1)
    }

    @IBAction func sliderG(sender: UISlider) {
        green = CGFloat(lroundf(sender.value))
        squareButton.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha/1)
    }

    @IBAction func sliderB(sender: UISlider) {
        blue = CGFloat(lroundf(sender.value))
        squareButton.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha/1)
    }

    @IBAction func sliderAlpha(sender: UISlider) {
        alpha = CGFloat(sender.value)
        squareButton.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha/1)
    }

    @IBAction func squareButton(sender: UIButton) {
        squareButton.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        red = CGFloat(255)
        green = CGFloat(255)
        blue = CGFloat(255)
        alpha = CGFloat(1)
    }
}

So I was practicing using four UISlider to control a button's background RGB color and opacity (1.R/ 2.G/ 3.B/ 4.alpha). And I was trying to make the exact button a reset button.

I did manage to change the button's background color and opacity, and when hitting reset button the background color goes back to white as well. But as everything goes back to the initial state, the slider's thumb still stay where there are.

What should I do to make the thumbs go back to the initial position as I hit reset?

Also if there is any mistake or anything can be done better please let me know. I am very new to Swift and desperate to learn. Thank you!

Create IBOutlets for your sliders and connect them in the storyboard.

When the reset button is pressed, set the value of the sliders to the original value.

redSlider.value = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM