为什么SkiaSharp Touch SKTouchAction.Moved事件不起作用?

时间:2019-03-18 18:12:57

标签: xamarin xamarin.forms touch skiasharp

摘要

使用SKTouchAction.Moved的ActionType永远不会引发Touch事件,但是会引发SKTouchAction.Pressed。为什么.Moved事件永远不会引发?

详细信息

我正在尝试在SkiaSharp中创建一个滑块。当用户触摸滑块或拖动时,此控件将需要更新 thumb (小的可移动圆圈)。它托管在Xamarin Forms应用程序中,并且我创建了SKCanvasView来绘制滑块并响应触摸事件。

我已经成功地响应了触摸事件,所以我知道SKCanvasView正在接收一些UI事件,但是从未引发SKTouchAction.Moved

示例代码

private SKCanvasView CreateSliderControl()
{
    var control = new SKCanvasView();
    control.PaintSurface += HandlePaintHeightControl;
    control.EnableTouchEvents = true;
    control.Touch += (sender, args) =>
    {
        var pt = args.Location;

        switch (args.ActionType)
        {
            case SKTouchAction.Pressed:
                // 1. This code gets hit whenever I touch the canvas
                control.InvalidateSurface();
                break;
            case SKTouchAction.Moved:
                // 2. This code never gets hit, even if I push, touch, slide, etc
                control.InvalidateSurface();
                break;
        }
    };
    control.InputTransparent = false;

    return control;
}

如上所示,#1被击中(我可以在此处放置一个断点,并且我的控制面无效)。 #2永远不会被击中,但是我希望当我将手指滑到控件上时会被击中。

我尝试过的东西

1 个答案:

答案 0 :(得分:1)

在SkiaSharp的Github上找到了similar issue

通过将事件的args中的Handled属性设置为true,尝试告诉OS您要“继续接收触摸事件”。

private SKCanvasView CreateSliderControl()
{
    var control = new SKCanvasView();
    control.PaintSurface += HandlePaintHeightControl;
    control.EnableTouchEvents = true;
    control.Touch += (sender, args) =>
    {
        var pt = args.Location;

        switch (args.ActionType)
        {
            case SKTouchAction.Pressed:
                control.InvalidateSurface();
                break;
            case SKTouchAction.Moved:
                control.InvalidateSurface();
                break;
        }
        // Let the OS know that we want to receive more touch events
        args.Handled = true;
    };
    control.InputTransparent = false;

    return control;
}