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

舊的方法,沒有更新:) –