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

因此,我正在练习使用四个UISlider来控制按钮的背景RGB颜色和不透明度(1.R / 2.G / 3.B / 4.alpha)。我试图将确切的按钮设为重置按钮。

我确实设法更改了按钮的背景色和不透明度,并且在单击“重置”按钮时,背景色也变回了白色。但是,当一切恢复到初始状态时,滑块的拇指仍停留在原处。

当我按下复位键时,应该怎么做才能使拇指回到初始位置?

另外,如果有任何错误或可以做得更好的任何事情,请告诉我。我对Swift非常陌生,渴望学习。谢谢!

最佳答案

为滑块创建IBOutlet,并将其连接到情节提要中。

按下复位按钮后,将滑块的值设置为原始值。

redSlider.value = 1;

关于ios - 将UISlider拇指重置为初始位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32316826/

10-12 06:28