两个UISlider和UILabel以编程方式设置它们,但不知道哪个滑块是幻灯片

时间:2017-11-09 17:31:59

标签: ios swift uislider

我有一个iOS项目,我在其中设置了两个滑块和相应的标签。 标签用于滑块值。当它们滑动时,标签中的值会发生变化。

我有另一个类,我在创建并在数组中保存该实例后设置滑块和标签。我遇到的困难是当我滑动滑块时,我不知道哪个滑块是幻灯片。

这是代码:

SliderWidget类

class SliderWidget {

    private var label: UILabel?
    private var slider: UISlider?

    public func setLable(label: UILabel) {
        self.label = label
    }

    public func setSlider(slider: UISlider) {
        self.slider = slider
    }

    public func getLabel() -> UILabel {
        return self.label!
    }

    public func getSlider() -> UISlider {
        return slider!
    }
}

这是我设置滑块和标签

的控制器
class WidgetsVC: UIViewController {

    private let step: Float = 10
    private var labels: UILabel?
    private var sliderArray: Array<SliderWidget> = Array()

    override func viewDidLoad() {
        super.viewDidLoad()

        displaySlider(sliderY: 100,labelY: 70)
        displaySlider(sliderY: 200, labelY: 170)
    }

    private func displaySlider(sliderY: Int, labelY: Int) {

        let slider = UISlider()
        slider.tintColor = #colorLiteral(red: 0.2980392277, green: 0.6862744689, blue: 0.3137254715, alpha: 1)

        let label = displayLabel(labelY: labelY, slider: slider)
        view.addSubview(slider)

        let horizonalContraints =
            NSLayoutConstraint(item: slider, attribute: .leadingMargin,
                               relatedBy: .equal, toItem: view,
                               attribute: .leadingMargin, multiplier: 1.0,
                               constant: 20)

        let horizonal2Contraints =
            NSLayoutConstraint(item: slider, attribute: .trailingMargin,
                               relatedBy: .equal, toItem: view,
                               attribute: .trailingMargin, multiplier: 1.0,
                               constant: -20)

        let pinTop =
            NSLayoutConstraint(item: slider, attribute: .top,
                               relatedBy: .equal, toItem: view,
                               attribute: .top, multiplier: 1.0,
                               constant: CGFloat(sliderY))

        slider.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([horizonalContraints, horizonal2Contraints, pinTop])

        slider.addTarget(self, action: #selector(WidgetsVC.sliderValueDidChange(_:)), for: .valueChanged)
    }

    private func displayLabel(labelY: Int, slider: UISlider) -> UILabel {

        let label = UILabel()
        label.text = "0"
        labels = label
        view.addSubview(label)

        let temp = SliderWidget()
        temp.setLable(label: label)
        temp.setSlider(slider: slider)
        sliderArray.append(temp)

        let horizonalContraints =
            NSLayoutConstraint(item: label, attribute: .leadingMargin,
                               relatedBy: .equal, toItem: view,
                               attribute: .leadingMargin, multiplier: 1.0,
                               constant: 20)

        let pinTop =
            NSLayoutConstraint(item: label, attribute: .top,
                               relatedBy: .equal, toItem: view,
                               attribute: .top, multiplier: 1.0,
                               constant: CGFloat(labelY))

        label.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([horizonalContraints, pinTop])

        return label
    }

}

2 个答案:

答案 0 :(得分:0)

我不确定你究竟想要什么,但你可以这样做

@objc func sliderValueDidChange(slider: UISlider) {
    print(slider.tag)
    let changedSliderWidget = sliderArray.filter { (sliderWidget) -> Bool in
      return sliderWidget.getSlider() == slider
    }.last
  }

changedSliderWidget将包含您需要的小部件

答案 1 :(得分:-1)

识别每个标签的简单方法是设置标签值:

displaySlider(sliderY: 100,labelY: 70, tag: 1)
displaySlider(sliderY: 200, labelY: 170, tag: 2)

private func displayLabel(labelY:Int,slider:UISlider, tag: Int)->UILabel{
    let label=UILabel()
    label.tag = tag
    label.text="0"
    ....

因此,您可以使用.tag值识别滑块。