6

从这个post我才知道,存在着一些平台的改进为 实施捏和缩放功能。通过使用这种新方法(ManipulationDeltaEventArgs.PinchManipulation),我可以如何实现在Windows Phone中捏合缩放功能。指缩放功能在Windows手机8

除了这个我需要实现滚动功能太图像控件。在我当前的实现中,我使用Toolkit(手势监听器)来实现捏合和缩放功能以及滚动查看器,现在看起来滚动和捏合事件都是重叠的,因此会产生不良的用户体验。

任何人都可以帮助我解决这个问题,在我的应用程序。我正在寻找一些帮助我实现功能的代码示例。

我不有望获得多点触摸行为(CodePlex上)的答案。在项目中使用的程序集相当老旧,我听说其中很多人正面临着市场提交的问题,仅此而已。

回答

28

正如我在previous answer说,如果你正在构建一个WP8独家应用程序,你可以使用新的ManipulationDeltaEventArgs.PinchManipulation的捏&缩放效果。以下是如何使用ManipulationDeltaEventArgs.PinchManipulation数据缩放,移动和旋转图像的基本示例。

首先,我们将创建一个基本的图像徘徊在网格中间:

<Grid x:Name="ContentPanel"> 
    <Image Source="Assets\Headset.png" 
      Width="200" Height="150" 
      ManipulationDelta="Image_ManipulationDelta" 
      x:Name="img" 
      > 
     <Image.RenderTransform> 
      <CompositeTransform CenterX="100" CenterY="75" /> 
     </Image.RenderTransform> 
    </Image> 
</Grid> 

接下来,我们会处理在ManipulationDelta事件,检查它是否是一个捏操作和应用正确的Silverlight转换在我们的UIElement。

private void Image_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) 
{ 
    if (e.PinchManipulation != null) 
    { 
     var transform = (CompositeTransform)img.RenderTransform; 

     // Scale Manipulation 
     transform.ScaleX = e.PinchManipulation.CumulativeScale; 
     transform.ScaleY = e.PinchManipulation.CumulativeScale; 

     // Translate manipulation 
     var originalCenter = e.PinchManipulation.Original.Center; 
     var newCenter = e.PinchManipulation.Current.Center; 
     transform.TranslateX = newCenter.X - originalCenter.X; 
     transform.TranslateY = newCenter.Y - originalCenter.Y; 

     // Rotation manipulation 
     transform.Rotation = angleBetween2Lines(
      e.PinchManipulation.Current, 
      e.PinchManipulation.Original); 

     // end 
     e.Handled = true; 
    } 
} 

// copied from http://www.developer.nokia.com/Community/Wiki/Real-time_rotation_of_the_Windows_Phone_8_Map_Control 
public static double angleBetween2Lines(PinchContactPoints line1, PinchContactPoints line2) 
{ 
    if (line1 != null && line2 != null) 
    { 
     double angle1 = Math.Atan2(line1.PrimaryContact.Y - line1.SecondaryContact.Y, 
            line1.PrimaryContact.X - line1.SecondaryContact.X); 
     double angle2 = Math.Atan2(line2.PrimaryContact.Y - line2.SecondaryContact.Y, 
            line2.PrimaryContact.X - line2.SecondaryContact.X); 
     return (angle1 - angle2) * 180/Math.PI; 
    } 
    else { return 0.0; } 
} 

这就是我们所做的:

  • 缩放: PinchManipulation实际轨道缩放我们,所以我们必须做的是应用PinchManipulation.CumulativeScale的缩放因子。
  • 变换: PinchManipulation跟踪原始中心和新中心(两个触摸点之间的计算)。通过从旧中心减去新中心,我们可以知道UIElement需要移动多少并将其应用于平移变换。请注意,此处更好的解决方案还可以通过跟踪此代码没有的累积原始中心来解决多个操作会话。
  • 旋转:我们想出了两个触摸点之间的角度和应用它作为旋转变换。更多关于这诺基亚wiki文章中@Real-time rotation of the Windows Phone 8 Map Control

这里是显示这个代码的一些打印屏幕运行良好:

Untouched image Rotated, transformed and scaled image Rotated, transformed and scaled image

+0

谢谢贾斯汀,详细和整洁的答案.. :) – StezPet

+2

嗨,贾斯汀,我试过你的实现,我认为用户体验不是那么好。你能解释一下这个程序来实现像wp8原生照片查看器这样的照片查看器吗?像支持缩放,滚动等的照片浏览器。我试着用scrollviever编码,但我注意到获得滚动体验。甚至在添加滚动查看器后捏和缩放都不起作用。 –

+0

@Justin真的很棒,很容易使用,但我无法在模拟器上测试它是否有任何方法在模拟器上测试它? – Mohit

-2

如果你正在寻找推出自己的,你可能想看看这篇文章捏在Silverlight放大:Implementing pinch zoom correctly in Silverlight

Telerik的也有一个开箱即平移和缩放图像控制的,但它确实花费金钱:Telerik Pan and Zoom Control

+0

我正在寻找WP8的新功能。我认为手势监听器现在已被弃用。此外,我尝试telerik,我认为这是不可行的,通过teleric控制只为实现这一功能更多的滚动功能是不可用的telerik控制。 – StezPet

+0

旧的方法,没有更新:) –