MR.Gestures - 自定义视图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MR.Gestures - 自定义视图相关的知识,希望对你有一定的参考价值。

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

我的猜测是继承IGesture Aware Control

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。它使用一个Effect来支持触摸操作,更多关于效果的细节: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
}

以上是关于MR.Gestures - 自定义视图的主要内容,如果未能解决你的问题,请参考以下文章

在片段中创建自定义列表视图时出错所需活动,找到片段

片段中的按钮自定义视图

android MVP - 我可以有多个演示者用于自定义视图和片段

Android:在片段内膨胀自定义视图

为片段制作自定义列表视图?

如何在片段内创建自定义列表视图