将UISlider Thumb重置为初始位置

时间:2015-08-31 17:37:12

标签: ios iphone xcode swift

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)。我试图让确切的按钮成为重置按钮。

我确实设法更改了按钮的背景颜色和不透明度,当按下重置按钮时,背景颜色也会变回白色。但是当一切都回到初始状态时,滑块的拇指仍然留在那里。

当我点击重置时,我该怎么做才能让拇指回到初始位置?

如果有任何错误或任何事情可以做得更好,请告诉我。我对斯威夫特很陌生,不顾一切地学习。谢谢!

1 个答案:

答案 0 :(得分:3)

为幻灯片创建IBOutlets并将其连接到故事板中。

按下重置按钮时,将滑块的值设置为原始值。

redSlider.value = 1;