1

Skiasharp 可以使用以下方法操作触摸事件:

private void OnTouch(object sender, SKTouchEventArgs args)

使用这种方法,我只能处理一个触摸点坐标:

args.Location.X, args.Location.Y

但是如何检测多点触控事件?

例如,Android 原生 API 使您能够管理两个手指位置以检测多点触控。

我如何使用 SkiaSharp 实现这一目标?

4

1 回答 1

4

好的,我在这里找到了我的问题的解决方案是代码。

//多点触控处理

Dictionary<long, SKPoint> dragDictionary = new Dictionary<long, SKPoint>();

    private void OnTouch(object sender, SKTouchEventArgs e)
    {
        switch (e.ActionType)
        {
            case SKTouchAction.Pressed:
                dragDictionary[e.Id] = e.Location;
                break;
            case SKTouchAction.Entered:
                break;
            case SKTouchAction.Moved:
                if (dragDictionary.Keys.Count > 1)
                {
                    dragDictionary[e.Id] = e.Location;
                    SKPoint? p1 = null;
                    SKPoint? p2 = null;
                    foreach (long key in dragDictionary.Keys)
                    {
                        if (p1 == null)
                        {
                            p1 = dragDictionary[key];
                        }
                        else if (p2 == null)
                        {
                            p2 = dragDictionary[key];
                        }
                    }
                    //MultiTouch handle
                }
                else
                {
                    //SingleTouch handle
                }
                break;
            case SKTouchAction.Released:
                dragDictionary.Remove(e.Id);
                break;
            case SKTouchAction.Exited:
                break;
        }
        // we have handled these events
        e.Handled = true;
        ((SKCanvasView)sender).InvalidateSurface();
    }
于 2017-12-20T16:41:40.867 回答