MR.Gestures - 自定义视图

MR.Gestures - Custom View

我有一个 SKCanvasView 可以绘制一些图形,现在需要获取点击位置。在阅读了几篇文章后,我决定通过 MR.Gestures 来实现它。因为包里面没有SKCanvasView的标准实现,需要自己实现。

我的猜测是继承自 IGestureAwareControl

public class AudioWaveView : SKCanvasView, IGestureAwareControl

这个需要实现一堆事件和命令:

    public GestureHandler GestureHandler { get; }
    public ICommand DownCommand { get; set; }
    public object DownCommandParameter { get; set; }
    public ICommand UpCommand { get; set; }
    public object UpCommandParameter { get; set; }
    public ICommand TappingCommand { get; set; }
    public object TappingCommandParameter { get; set; }
    public ICommand TappedCommand { get; set; }
    public object TappedCommandParameter { get; set; }
    public ICommand DoubleTappedCommand { get; set; }
    public object DoubleTappedCommandParameter { get; set; }
    public ICommand LongPressingCommand { get; set; }
    public object LongPressingCommandParameter { get; set; }
    public ICommand LongPressedCommand { get; set; }
    public object LongPressedCommandParameter { get; set; }
    public ICommand PinchingCommand { get; set; }
    public object PinchingCommandParameter { get; set; }
    public ICommand PinchedCommand { get; set; }
    public object PinchedCommandParameter { get; set; }
    public ICommand PanningCommand { get; set; }
    public object PanningCommandParameter { get; set; }
    public ICommand PannedCommand { get; set; }
    public object PannedCommandParameter { get; set; }
    public ICommand SwipedCommand { get; set; }
    public object SwipedCommandParameter { get; set; }
    public ICommand RotatingCommand { get; set; }
    public object RotatingCommandParameter { get; set; }
    public ICommand RotatedCommand { get; set; }
    public object RotatedCommandParameter { get; set; }
    public event EventHandler<DownUpEventArgs> Down;
    public event EventHandler<DownUpEventArgs> Up;
    public event EventHandler<TapEventArgs> Tapping;
    public event EventHandler<TapEventArgs> Tapped;
    public event EventHandler<TapEventArgs> DoubleTapped;
    public event EventHandler<LongPressEventArgs> LongPressing;
    public event EventHandler<LongPressEventArgs> LongPressed;
    public event EventHandler<PinchEventArgs> Pinching;
    public event EventHandler<PinchEventArgs> Pinched;
    public event EventHandler<PanEventArgs> Panning;
    public event EventHandler<PanEventArgs> Panned;
    public event EventHandler<SwipeEventArgs> Swiped;
    public event EventHandler<RotateEventArgs> Rotating;
    public event EventHandler<RotateEventArgs> Rotated;

我不确定这是否是要走的路,但它看起来是一个很好的起点,但现在我被困住了,不知道如何走得更远。有没有人做过类似的东西或者知道解决方法?

非常感谢

请看Microsoft's Touch Manipulations article regarding SkiaSharp. It uses an Effect to support touch operations, more details on effects here: Invoking Events from Effects

基本上,您需要向添加了 CanvasView 的容器添加 TouchEffect(或者甚至向您的 CanvasView 添加效果,上面链接的文章中有这样的示例):

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

然后为 TouchAction 事件添加处理程序,如下所示。您需要先将触摸点转换为像素,然后检查是否

void OnTouchEffectAction(object sender, TouchActionEventArgs args)
{
        if (args.Type != TouchActionType.Pressed)
        {
            return;
        }

        var pointLocation = args.Location;
        var point =
            new SKPoint((float)(canvasView.CanvasSize.Width * pointLocation.X / canvasView.Width),
                        (float)(canvasView.CanvasSize.Height * pointLocation.Y / canvasView.Height));

        // TODO: Handle your touch here
}