如何使用 SkiaSharp 处理多点触控事件?赛马林
How to handle Multi-Touch events with SkiaSharp? Xamarin
Skiasharp 可以使用以下方法操作触摸事件:
private void OnTouch(object sender, SKTouchEventArgs args)
用这个方法我只能处理一个触摸点坐标:
args.Location.X, args.Location.Y
但是如何检测多点触控事件?
例如,Android 本机 API 使您能够管理两个手指位置以检测多点触控。
如何使用 SkiaSharp 实现这一目标?
好的,我找到了我的问题的解决方案,这里是代码。
//多点触控处理
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();
}
Skiasharp 可以使用以下方法操作触摸事件:
private void OnTouch(object sender, SKTouchEventArgs args)
用这个方法我只能处理一个触摸点坐标:
args.Location.X, args.Location.Y
但是如何检测多点触控事件?
例如,Android 本机 API 使您能够管理两个手指位置以检测多点触控。
如何使用 SkiaSharp 实现这一目标?
好的,我找到了我的问题的解决方案,这里是代码。
//多点触控处理
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();
}