在自定义滑块中创建更改值的操作

Create action of changing value in custom slider

我正在使用名为 EFCircularSlider 的框架并成功导入了 objective-c 文件,现在我在 Swift 中实现这行代码时遇到问题:

[slider addTarget:self action:@selector(newValue:) forControlEvents:UIControlEventValueChanged];

到目前为止,这是我的圆形滑块代码。

@IBOutlet var currentValueLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    let sliderFrame = CGRectMake(110, 150, 100, 100)
    let circularSlider = EFCircularSlider(frame: sliderFrame)
    self.view.addSubview(circularSlider)
}

我无法让它按照我在 Swift 中尝试编写的方式工作。我试过这样写:

slider.addTarget(self, action: "newValue:", forControlEvents: UIControlEvents.ValueChanged)

这是据说实现的方式 (https://github.com/eliotfowler/EFCircularSlider):

你需要在Swift中使用Selector,所以像这样:

slider.addTarget(self, action: Selector("newValue:"), forControlEvents: UIControlEvents.ValueChanged)

这里是全文viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()
    let sliderFrame = CGRectMake(110, 150, 100, 100)
    let circularSlider = EFCircularSlider(frame: sliderFrame)
    circularSlider.addTarget(self, action: Selector("newValue:"), forControlEvents: UIControlEvents.ValueChanged)
    self.view.addSubview(circularSlider)
}

确保您实施了 newValue 方法。像这样

func newValue(slider: EFCircularSlider) {
    // Do you stuff here
}