private void IllustrationOriginalImage_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) { var(deltaX, deltaY) = (e.Delta.Translation.X, e.Delta.Translation.Y); if (GetZoomFactor() > 1) { var renderedImageWidth = IllustrationOriginalImage.ActualWidth * GetZoomFactor(); var renderedImageHeight = IllustrationOriginalImage.ActualHeight * GetZoomFactor(); var containerWidth = IllustrationOriginalImageContainer.ActualWidth; var containerHeight = IllustrationOriginalImageContainer.ActualHeight; var imagePos = IllustrationOriginalImage.TransformToVisual(IllustrationOriginalImageContainer).TransformPoint(new Point(0, 0)); if (renderedImageWidth > containerWidth) { switch (deltaX) { case < 0 when imagePos.X > -(renderedImageWidth - containerWidth): case > 0 when imagePos.X < 0: IllustrationOriginalImageRenderTransform.TranslateX += deltaX; break; } } if (renderedImageHeight > containerHeight) { switch (deltaY) { case < 0 when imagePos.Y > -(renderedImageHeight - containerHeight): case > 0 when imagePos.Y < 0: IllustrationOriginalImageRenderTransform.TranslateY += deltaY; break; } } } }
private void IllustrationOriginalImage_OnManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e) { var renderedImageWidth = IllustrationOriginalImage.ActualWidth * GetZoomFactor(); var renderedImageHeight = IllustrationOriginalImage.ActualHeight * GetZoomFactor(); var containerWidth = IllustrationOriginalImageContainer.ActualWidth; var containerHeight = IllustrationOriginalImageContainer.ActualHeight; var pos = IllustrationOriginalImage.TransformToVisual(IllustrationOriginalImageContainer).TransformPoint(new Point(0, 0)); var sb = new Storyboard(); var animationDuration = TimeSpan.FromMilliseconds(200); const string translateXProperty = nameof(CompositeTransform.TranslateX); const string translateYProperty = nameof(CompositeTransform.TranslateY); var xLowerBound = -(renderedImageWidth - containerWidth); var yLowerBound = -(renderedImageHeight - containerHeight); switch (pos.X) { case var x when x <xLowerBound && renderedImageWidth> containerWidth: sb.Children.Add(IllustrationOriginalImageRenderTransform.CreateDoubleAnimation(translateXProperty, DoubleAnimationTemplate(xLowerBound - x))); break; case > 0 when renderedImageWidth > containerWidth: sb.Children.Add(IllustrationOriginalImageRenderTransform.CreateDoubleAnimation(translateXProperty, DoubleAnimationTemplate(-pos.X))); break; } switch (pos.Y) { case var y when y <yLowerBound && renderedImageHeight> containerHeight: sb.Children.Add(IllustrationOriginalImageRenderTransform.CreateDoubleAnimation(translateYProperty, DoubleAnimationTemplate(yLowerBound - y))); break; case > 0 when renderedImageHeight > containerHeight: sb.Children.Add(IllustrationOriginalImageRenderTransform.CreateDoubleAnimation(translateYProperty, DoubleAnimationTemplate(-pos.Y))); break; } if (sb.Children.Any()) { sb.Begin(); } Func <DoubleAnimation> DoubleAnimationTemplate(double by) => () => new DoubleAnimation { Duration = animationDuration, EasingFunction = _easingFunction, By = @by }; }