【问题标题】:Pinch-Zoom: Get Touch Position relative to Object捏缩放:获取相对于对象的触摸位置
【发布时间】:2015-12-08 07:03:07
【问题描述】:

我想制作一个触摸查看器,我想放大手指之间的点。

我在网格中有一个带有 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 时,它会在放大或缩小时将视图框的内容转换到其他位置。

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

【问题讨论】:

    标签: wpf touch multi-touch pinchzoom


    【解决方案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/的源码中发现了这个提示

    【讨论】:

      猜你喜欢
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 2014-04-22
      • 2012-10-20
      • 2017-02-11
      • 2012-09-08
      相关资源
      最近更新 更多