Question

How do I get the pinch-to-zoom x and y scaling values independent of each other for a Windows Store App? I'm currently using ManipulationDeltaRoutedEventArgs's ManipulationDelta structure, but as you can see it only offers a single scale.

// Global Transform used to change the position of the Rectangle.
private TranslateTransform dragTranslation;
private ScaleTransform scaleTransform;

// Constructor
public MainPage()
{
    InitializeComponent();

    // Add handler for the ManipulationDelta event
    TestRectangle.ManipulationDelta += Drag_ManipulationDelta;
    dragTranslation = new TranslateTransform();
    scaleTransform = new ScaleTransform();
    TestRectangle.RenderTransform = this.dragTranslation;
}

void Drag_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    // Move the rectangle.
    dragTranslation.X += e.Delta.Translation.X;
    dragTranslation.Y += e.Delta.Translation.Y;

    // Scaling, but I want X and Y independent!
    scaleTransform.ScaleX = e.Delta.Scale;
    scaleTransform.ScaleY = e.Delta.Scale;
}

XAML:

<Rectangle Name="TestRectangle"
  Width="200" Height="200" Fill="Blue" 
  ManipulationMode="All"/>

Code mostly taken from here.

Was it helpful?

Solution

I ended up using Handling Two, Three, Four Fingers Swipe Gestures in WinRT App to grab the coordinates of the two fingers, calculated the initial difference between them, and then scaled accordingly as the distance changed.

int numActiveContacts;
Dictionary<uint, int> contacts;
List<PointF> locationsOfSortedTouches;

void myCanvas_PointerPressed(object sender, PointerRoutedEventArgs e) {
  PointerPoint pt = e.GetCurrentPoint(myCanvas);
  locationsOfSortedTouches.Add(new PointF((float) pt.Position.X, (float) pt.Position.Y));
  touchHandler.TouchesBegan(locationsOfSortedTouches);
  contacts[pt.PointerId] = numActiveContacts;
  ++numActiveContacts;
  e.Handled = true;
}

void myCanvas_PointerMoved(object sender, PointerRoutedEventArgs e) {
  var pt = e.GetCurrentPoint(myCanvas);
  var ptrId = pt.PointerId;
  if (contacts.ContainsKey(ptrId)) {
    var ptrOrdinal = contacts[ptrId];
    Windows.Foundation.Point currentContact = pt.Position;
    locationsOfSortedTouches[ptrOrdinal] = new PointF((float) pt.Position.X, (float) pt.Position.Y);
    //distance calculation and zoom redraw here
  }
  e.Handled = true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top