WPF:通过触摸拖动元素非常不稳定
我有一个可以在某些边界内拖动的矩形。这与鼠标完美配合。
一旦我将 IsManipulationEnabled 设置为 true,鼠标事件就不再起作用。 但是我需要它来获取矩形上的触摸事件。因此我将其设置为true。
我正在尝试计算 ManipulationDelta 事件中的所有更改,如以下函数所示。 缩放效果已经相当不错,但是通过用手指拖动来移动对象非常不稳定,而且有时矩形会来回跳跃。
private void UserControl_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
//Scaling works already pretty good
RangeBar.Width *= e.DeltaManipulation.Scale.X;
//Moving the element is very choppy, what am I doing wrong here?
this.startX = this.startX + e.DeltaManipulation.Translation.X;
RangeBar.SetValue(Canvas.LeftProperty, startX);
}
I have a Rectangle which can be dragged within some boundaries. This works perfect with the mouse.
As soon as I set IsManipulationEnabled to true the mouse-events don't work anymore.
However I need this to get touch-events on the rectangle. Therefore I set it to true.
I'm trying to compute all changes in the ManipulationDelta-event like in the following function.
Scaling works already pretty good, but moving the object via dragging it with the finger is very choppy + plus sometimes the Rectangle jumps back and forth.
private void UserControl_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
//Scaling works already pretty good
RangeBar.Width *= e.DeltaManipulation.Scale.X;
//Moving the element is very choppy, what am I doing wrong here?
this.startX = this.startX + e.DeltaManipulation.Translation.X;
RangeBar.SetValue(Canvas.LeftProperty, startX);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试使用 CumulativeManipulation 代替。
每当我需要通过拖动进行 UI 元素移动时,我不会尝试重用相同的变量并按增量修改它,然后重用相同的变量来设置位置。无论平台如何,它几乎总是给我带来稳定性问题。相反,尝试在拖动开始时存储变量,并仅在需要更新位置时将增量添加到该变量。所以更像是这样的:
另外,ManipulationEvent 的来源是什么?您肯定需要确保它不是当前的矩形,否则肯定会导致您看到的问题。
I'd try using the CumulativeManipulation instead.
Whenever I need to do a UI element movement via dragging, I don't try to reuse the same variable and modify it by the delta and then reuse that same variable for setting position. It almost always gives me stability issues regardless of platform. Instead, try storing a variable when dragging starts and adding the delta to that variable only when you need to update position. So something more like this:
Also, what's the source of the ManipulationEvent? You'll definitely want to make sure it's not the current rectangle, or that will definitely cause the issues you're seeing.