简体   繁体   English

在 Xamarin.Forms 中使用 SkiaSharp 将点击事件添加到 SVG 中的元素

[英]Add tap events to elements inside an SVG using SkiaSharp in Xamarin.Forms

I want to get the particular points inside the svg for xamarin.forms Is there is any ways from which I can achieve?我想得到svg里面的特定点为xamarin.forms 请问有什么方法可以实现吗?

Thanks in advance.提前致谢。

Answering this old question to maybe help future explorers like myself - in my app I went with SkiaSharp's own tap handlers that are on SKCanvas .回答这个老问题可能会帮助像我这样的未来探索者——在我的应用程序中,我使用了 SkiaSharp 自己的位于SKCanvas上的点击处理程序。 You turn them on with the EnableTouchEvents property and then set the callback in the Touch property.您使用EnableTouchEvents属性打开它们,然后在Touch属性中设置回调。 Also, in the callback handler make sure to set e.Handled = true;此外,在回调处理程序中确保设置e.Handled = true; on the SKTouchEventArgs or else you won't get the full spectrum of SKTouchAction types.SKTouchEventArgs上,否则您将无法获得完整的SKTouchAction类型。 Tap location is also included.水龙头位置也包括在内。

Yes you can add touch events to particular points using Skiasharp. 是的,您可以使用Skiasharp将触摸事件添加到特定点。 You just have to filter actions by Locations. 您只需要按位置过滤操作即可。 Assuming you are using Xaml and C#, you Xaml code would look like this 假设您正在使用Xaml和C#,则Xaml代码将如下所示

<Grid BackgroundColor="White"
        Grid.Row="1">

    <skia:SKCanvasView x:Name="canvasView"
                        PaintSurface="OnCanvasViewPaintSurface" />
    <Grid.Effects>
        <tt:TouchEffect Capture="True"
                        TouchAction="OnTouchEffectAction" />
    </Grid.Effects>
</Grid>

and then your C# would be like this 然后您的C#会像这样

void OnTouchEffectAction(object sender, TouchActionEventArgs args)
{
    // Convert Xamarin.Forms point to pixels
    Point pt = args.Location;
    SKPoint point = 
        new SKPoint((float)(canvasView.CanvasSize.Width * pt.X / canvasView.Width),
                    (float)(canvasView.CanvasSize.Height * pt.Y / canvasView.Height));

    // Uncomment the code below based on what you specifically need
    // if (point != The Point you care about) or (pt != The Location you care about
        // return;
    switch (args.Type)
    {
        case TouchActionType.Pressed:
            if (bitmap.HitTest(point))
            {
                touchIds.Add(args.Id);
                bitmap.ProcessTouchEvent(args.Id, args.Type, point);
                break;
            }
            break;

        case TouchActionType.Moved:
            if (touchIds.Contains(args.Id))
            {
                bitmap.ProcessTouchEvent(args.Id, args.Type, point);
                canvasView.InvalidateSurface();
            }
            break;

        case TouchActionType.Released:
        case TouchActionType.Cancelled:
            if (touchIds.Contains(args.Id))
            {
                bitmap.ProcessTouchEvent(args.Id, args.Type, point);
                touchIds.Remove(args.Id);
                canvasView.InvalidateSurface();
            }
            break;
    }
}

Charles Petzold authored these Github examples with Skiasharp, check them out to get a better idea Charles Petzold与Skiasharp一起撰写了这些Github示例 ,请查看它们以获取更好的主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM