Swift / SnappingSlider /滑块的两个实例

时间:2020-11-29 18:09:23

I'm using the SnappingSlider. I want to use two instances of the Slider and be able to change different values depending on the used Slider.

我正在使用SnappingSlider。我想使用Slider的两个实例,并且能够根据使用的Slider更改不同的值。

This is my current code:

这是我目前的代码:

import UIKit
import SnappingSlider

class ViewController: UIViewController, SnappingSliderDelegate {

private var remainingTime:Double = 0
private let remainingTimeLabel:UILabel = UILabel(frame: CGRectZero)
private let remainingTimeSlider:SnappingSlider = SnappingSlider(frame: CGRectMake(0.0, 0.0, 10.0, 10.0), title: "Slide Me")
private let percentageSlider:SnappingSlider = SnappingSlider(frame: CGRectMake(0.0, 0.0, 10.0, 10.0), title: "Slide Me")

override func viewDidLoad() {
    super.viewDidLoad()
    remainingTimeLabel.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width * 0.5, 80.0)
    remainingTimeLabel.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.21 - remainingTimeLabel.bounds.size.height * 0.75)
    remainingTimeLabel.font = UIFont(name: "TrebuchetMS-Bold", size: 25.0)
    remainingTimeLabel.textColor = UIColor.lightGrayColor()
    remainingTimeLabel.textAlignment = NSTextAlignment.Center
    remainingTimeLabel.text = "\(remainingTime)"
    self.view.addSubview(remainingTimeLabel)

    remainingTimeSlider.delegate = self
    remainingTimeSlider.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width * 0.9, 50.0)
    remainingTimeSlider.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.2)
    self.view.addSubview(remainingTimeSlider)

    percentageSlider.delegate = self
    percentageSlider.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width * 0.9, 50.0)
    percentageSlider.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.4)
    self.view.addSubview(percentageSlider)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func snappingSliderDidIncrementValue(slider: SnappingSlider) {

    remainingTime += 0.5
    remainingTimeLabel.text = "\(remainingTime)"
}

func snappingSliderDidDecrementValue(slider: SnappingSlider) {

    remainingTime = max(0, remainingTime - 0.5)
    remainingTimeLabel.text = "\(remainingTime)"
}


}

I want to do something like this:

我想做这样的事情:

func snappingSliderDidIncrementValue(slider: SnappingSlider) {

if slider == sliderA {
    remainingTime += 0.5
    remainingTimeLabel.text = "\(remainingTime)"
} else if slider == sliderB {
    integerXY = += 0.5
    labelXY.text = "\(remainingTime)"
}

I have send the author of the Slider an email and got the follwing text back: You can set a single object to act as a delegate for multiple instances of the slider. So, you’d set the .delegate property of the sliders to be whatever object you have conforming to the delegate and then you’d do the check inside of the delegate callbacks exactly how you’ve shown underneath.

我已经向Slider的作者发送了一封电子邮件并获得了以下文本:您可以设置一个对象作为滑块的多个实例的委托。因此,您将滑块的.delegate属性设置为您符合委托的任何对象,然后您将在委托回调中检查您在下面显示的内容。

But I don't get it... Help is very appreciated.

但我不明白......帮助非常感谢。

Edit:

Swift / SnappingSlider /滑块的两个实例

1 个解决方案

#1


1  

You need to do something like this:

你需要做这样的事情:

func snappingSliderDidIncrementValue(slider: SnappingSlider) {
    if slider == remainingTimeSlider {
        remainingTime += 0.5
        remainingTimeLabel.text = "\(remainingTime)"
    }
    else if slider == percentageSlider {
        // do something for this slider
    }
}

#1


1  

You need to do something like this:

你需要做这样的事情:

func snappingSliderDidIncrementValue(slider: SnappingSlider) {
    if slider == remainingTimeSlider {
        remainingTime += 0.5
        remainingTimeLabel.text = "\(remainingTime)"
    }
    else if slider == percentageSlider {
        // do something for this slider
    }
}