Xamarin iOS UISlider AddTarget EventHandler否UIEvent参数

时间:2018-09-25 20:41:08

标签: c# ios swift xamarin

我正在尝试转换一些快速代码,以检测UISlider控件何时开始拖动拇指,然后何时将其拖动到Xamarin.iOS C#中。但是Xamarin.iOS代码的EventHandler不会传递UIEvent参数。

迅速:

slider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)


@objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
if let touchEvent = event.allTouches?.first {
    switch touchEvent.phase {
    case .began:
        // handle drag began
    case .moved:
        // handle drag moved
    case .ended:
        // handle drag ended
    default:
        break
    }
}
}

C#

 slider.AddTarget(HandleEventHandler, UIControlEvent.ValueChanged);

 void HandleEventHandler(object sender, EventArgs e)
 {
    //TODO
 }

1 个答案:

答案 0 :(得分:0)

xamarin.ios与Swift不同,它基于OC。 UISlider click事件不允许采用多个参数,唯一的参数是默认的UISlider本身。如果要区分不同的状态。您可以addTarget处于不同状态。

slider.AddTarget(this,new Selector("SliderValueChange:"),UIControlEvent.ValueChanged);
slider.AddTarget(this,new Selector("SliderTouchStart:"), UIControlEvent.TouchDown);
slider.AddTarget(this,new Selector("SliderTouchEnded:"), UIControlEvent.TouchUpInside);

[Export("SliderValueChange:")]
public void SliderValueChange(UISlider sender)
 {

    Console.WriteLine(sender.Value);

 }


[Export("SliderTouchStart:")]
public void SliderTouchStart(UISlider sender)
 {

    Console.WriteLine("start");

 }

[Export("SliderTouchEnded:")]
public void SliderTouchEnded(UISlider sender)
 {

    Console.WriteLine("end");

 }