捏缩放:相对于对象获取触摸位置

时间:2015-12-08 07:03:07

标签: wpf touch multi-touch pinchzoom

我想制作一个Touch-Viewer,我想放大手指之间的点。

我在网格中有一个带有content-presenter的视图框:

<Grid IsManipulationEnabled="True">
   <Viewbox x:Name="PART_Viewbox">
      /// Content
   </Viewbox>
</Grid>

使用我的鼠标滚轮放大到鼠标所在的位置,我用以下方法解决:

var position = e.GetPosition(PART_Viewbox);
matrix.ScaleAtPrepend(scale, scale, position.X, position.Y);

方法 e.GetPosition 仅适用于 MouseEventsArgs ,而不适用于 ManipulationDeltaEventArgs

那么如何在触摸模式中获得相对位置?

当我从 ManipulationDeltaEventArgs 获取 e.ManipulationOrigin 并且网格更大时,Viewbox会在放大或缩小时将视图框的内容转换为其他位置。< / p>

大多数示例显示缩放到内容(图像)的中间。那不是我想要的。

1 个答案:

答案 0 :(得分:1)

FrameworkElement类派生自UIElement。有一种方法TranslatePoint可以转换相对于元素的点。

ManipulationDeltaEventArgs 中,属性 ManipulationOrigin 表示捏缩放中两个手指之间的点。另一个重要的属性是 ManipulationContainer ,其中包含执行操作的元素。

所以在我的情况下我可以做以下事情:

// Typecast ManipulationContainer to FrameworkElement and get point around the finger
Point position =((FrameworkElement)e.ManipulationContainer)
.TranslatePoint(e.ManipulationOrigin, _Viewbox);

// Center the new point in account to previous manipulations
position = matrix.Transform(position);

// do the transformation
matrix.ScaleAt(deltaManipulation.Scale.X, deltaManipulation.Scale.Y, position.X, position.Y)

我在https://multitouchtransformbehavior.codeplex.com/

的源代码中找到了这个提示
相关问题