如何在uwp中获得触摸输入?

时间:2016-08-26 13:20:57

标签: c# wpf windows-phone uwp

我需要如果用户用一根手指触摸画布控件,绘图过程必须开始,如果用户用两根手指触摸画布控件,画布区域必须滚动/平移。我怎么能这样做?

由于

1 个答案:

答案 0 :(得分:1)

我们可以使用指针事件来检测UWP中的多个手指输入。例如:

func scrollViewDidScroll(scrollView: UIScrollView) {
    if tb_time1 == scrollView {
        tb_time2.contentOffset = tb_time1.contentOffset
    }else if tb_time2 == scrollView {
        tb_time1.contentOffset = tb_time2.contentOffset
    }
}

如您所见,我将<ScrollViewer x:Name="scrollViewer" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" VerticalScrollMode="Disabled" HorizontalScrollMode="Disabled"> <canvas:CanvasControl x:Name="canvasControl" Draw="CanvasControl_Draw" CreateResources="canvasControl_CreateResources" ClearColor="CornflowerBlue" PointerPressed="canvasControl_PointerPressed" PointerReleased="canvasControl_PointerReleased" PointerExited="canvasControl_PointerReleased" PointerCanceled="canvasControl_PointerPressed" PointerMoved="canvasControl_PointerMoved" Width="1200"> </canvas:CanvasControl> </ScrollViewer> 放出ScrollViewer,因此画布也可以滚动。代码背后:

canvas:CanvasControl

当用一根手指绘制时,在我的演示中,画布将仅在指针释放时更新其布局。您可以更改代码,以便在每次移动指针时更新画布。但我认为,为此,您需要将private CanvasRenderTarget renderTarget; private async void CanvasControl_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { args.DrawingSession.DrawImage(renderTarget); } private void canvasControl_CreateResources(CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args) { renderTarget = new CanvasRenderTarget(sender, (float)sender.ActualWidth, (float)sender.ActualHeight); } private List<PointerPoint> m_pt = new List<PointerPoint>(); private void canvasControl_PointerPressed(object sender, PointerRoutedEventArgs e) { PointerPoint cp = e.GetCurrentPoint(canvasControl); m_pt.Add(cp); if (m_pt.Count == 2) { scrollViewer.HorizontalScrollMode = ScrollMode.Enabled; scrollViewer.VerticalScrollMode = ScrollMode.Enabled; } } private void canvasControl_PointerReleased(object sender, PointerRoutedEventArgs e) { if (m_pt.Count == 1) canvasControl.Invalidate(); scrollViewer.HorizontalScrollMode = ScrollMode.Disabled; scrollViewer.VerticalScrollMode = ScrollMode.Disabled; m_pt.Clear(); } private void canvasControl_PointerMoved(object sender, PointerRoutedEventArgs e) { if (m_pt.Count == 1) { var pt = e.GetCurrentPoint(canvasControl); using (var ds = renderTarget.CreateDrawingSession()) { ds.DrawCircle(new Vector2((float)pt.Position.X, (float)pt.Position.Y), 2, Colors.Black); } } else { if (m_pt.Count > 2) m_pt.Clear(); } } 替换为另一个Win2D控件,有关详细信息,请参阅CanvasControl.Invalidate Method