gpt4 book ai didi

c# - 检测简单的触摸手势

转载 作者:IT王子 更新时间:2023-10-29 04:33:49 30 4
gpt4 key购买 nike

谁能解释一下如何在 WinRT 应用程序中检测简单的触摸手势?我尝试使用 GestureRecognizer 类,但没有成功:

    public MainPage()
{
this.InitializeComponent();
Windows.UI.Input.GestureRecognizer gr = new Windows.UI.Input.GestureRecognizer();
gr.CrossSliding += gr_CrossSliding;
gr.Dragging += gr_Dragging;
gr.Holding += gr_Holding;
gr.ManipulationCompleted += gr_ManipulationCompleted;
gr.ManipulationInertiaStarting += gr_ManipulationInertiaStarting;
gr.ManipulationStarted += gr_ManipulationStarted;
gr.ManipulationUpdated += gr_ManipulationUpdated;
gr.RightTapped += gr_RightTapped;
gr.Tapped += gr_Tapped;
gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | Windows.UI.Input.GestureSettings.ManipulationTranslateY |
Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | Windows.UI.Input.GestureSettings.ManipulationScaleInertia |
Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | Windows.UI.Input.GestureSettings.Tap;

}

void gr_Tapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.TappedEventArgs args)
{
Debug.WriteLine("gr_Tapped");
}
void gr_RightTapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.RightTappedEventArgs args)
{
Debug.WriteLine("gr_RightTapped");
}
void gr_Holding(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.HoldingEventArgs args)
{
Debug.WriteLine("gr_Holding");
}
void gr_Dragging(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.DraggingEventArgs args)
{
Debug.WriteLine("gr_Dragging");
}
void gr_CrossSliding(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.CrossSlidingEventArgs args)
{
Debug.WriteLine("gr_CrossSliding");
}
void gr_ManipulationUpdated(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationUpdatedEventArgs args)
{
Debug.WriteLine("gr_ManipulationUpdated");
}
void gr_ManipulationStarted(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationStartedEventArgs args)
{
Debug.WriteLine("gr_ManipulationStarted");
}
void gr_ManipulationCompleted(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationCompletedEventArgs args)
{
Debug.WriteLine("gr_ManipulationCompleted");
}
void gr_ManipulationInertiaStarting(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationInertiaStartingEventArgs args)
{
Debug.WriteLine("gr_ManipulationInertiaStarting");
}

最佳答案

如果您注意到 MainPage 类有自己的操作事件,您无需创建单独的 GestureRecognizer 即可使用。您可以通过将 this.ManipulationMode 设置为 ManipulationModes.All 来启用它。这将允许您在 MainPages TappedRightTappedManipulationStartingManipulationStartedManipulationDelta 上看到响应ManipulationCompleted 事件。

至于让 GestureRecongnizer 根据此 Blog 工作还有这个MSDN Forum Posting您将需要像这样处理 MainPage 的 PointerMovedPointerReleasedPointerPressed 事件。

Windows.UI.Input.GestureRecognizer gr = new Windows.UI.Input.GestureRecognizer();  

public MainPage()
{
this.InitializeComponent();
this.PointerPressed += MainPage_PointerPressed;
this.PointerMoved += MainPage_PointerMoved;
this.PointerReleased += MainPage_PointerReleased;
gr.CrossSliding += gr_CrossSliding;
gr.Dragging += gr_Dragging;
gr.Holding += gr_Holding;
gr.ManipulationCompleted += gr_ManipulationCompleted;
gr.ManipulationInertiaStarting += gr_ManipulationInertiaStarting;
gr.ManipulationStarted += gr_ManipulationStarted;
gr.ManipulationUpdated += gr_ManipulationUpdated;
gr.RightTapped += gr_RightTapped;
gr.Tapped += gr_Tapped;
gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | Windows.UI.Input.GestureSettings.ManipulationTranslateY |
Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | Windows.UI.Input.GestureSettings.ManipulationScaleInertia |
Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | Windows.UI.Input.GestureSettings.Tap;
}

void MainPage_PointerReleased(object sender, PointerRoutedEventArgs e)
{
var ps = e.GetIntermediatePoints(null);
if (ps != null && ps.Count > 0)
{
gr.ProcessUpEvent(ps[0]);
e.Handled = true;
gr.CompleteGesture();
}
}

void MainPage_PointerMoved(object sender, PointerRoutedEventArgs e)
{
gr.ProcessMoveEvents(e.GetIntermediatePoints(null));
e.Handled = true;
}

void MainPage_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var ps = e.GetIntermediatePoints(null);
if (ps != null && ps.Count > 0)
{
gr.ProcessDownEvent(ps[0]);
e.Handled = true;
}
}

根据Documentation你需要启用 CrossSlide通过将事件添加到您的 GestureRecongnizer 并设置 CrossSlideThresholds和方向。来自最后一个链接:

CrossSlide must be set in the GestureSettings property to support CrossSliding. CrossSliding distance thresholds are disabled by default. Use CrossSlideThresholds to set these values.

例子:

Windows.UI.Input.CrossSlideThresholds cst = new Windows.UI.Input.CrossSlideThresholds();
cst.SelectionStart = 2;
cst.SpeedBumpStart = 3;
cst.SpeedBumpEnd = 4;
cst.RearrangeStart = 5;
gr.CrossSlideHorizontally = true;
gr.CrossSlideThresholds = cst;
gr.CrossSliding += gr_CrossSliding;

并确保它已添加到您的GestureSettings

gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX |
Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia |
Windows.UI.Input.GestureSettings.ManipulationScaleInertia | Windows.UI.Input.GestureSettings.ManipulationTranslateInertia |
Windows.UI.Input.GestureSettings.Tap | Windows.UI.Input.GestureSettings.CrossSlide;

关于c# - 检测简单的触摸手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12269725/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com