WPF:通过触摸拖动元素非常不稳定

发布于 2024-10-29 12:33:06 字数 616 浏览 0 评论 0原文

我有一个可以在某些边界内拖动的矩形。这与鼠标完美配合。

一旦我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷了相思 2024-11-05 12:33:06

我会尝试使用 CumulativeManipulation 代替。

每当我需要通过拖动进行 UI 元素移动时,我不会尝试重用相同的变量并按增量修改它,然后重用相同的变量来设置位置。无论平台如何,它几乎总是给我带来稳定性问题。相反,尝试在拖动开始时存储变量,并仅在需要更新位置时将增量添加到该变量。所以更像是这样的:

Point origin;

void MouseDown(Point location)
{
   origin = location;
}

void MouseDrag(Vector cumulativeOffset)
{
   SetControlLocation(origin+cumulativeOffset);
}

另外,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:

Point origin;

void MouseDown(Point location)
{
   origin = location;
}

void MouseDrag(Vector cumulativeOffset)
{
   SetControlLocation(origin+cumulativeOffset);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文