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

时间:2016-03-13 12:11:16

标签: objective-c swift events slider action

我正在使用一个名为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): enter image description here

1 个答案:

答案 0 :(得分:1)

你需要在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
}