【发布时间】:2019-12-11 15:05:56
【问题描述】:
我对通过操纵事件(更准确地说是矩阵)进行对象转换有疑问。
问题:在我的对象(顺便说一句:一个矩形)第一次成功旋转/缩放/平移后,第二次操作(在本例中为平移)将在其新角度的方向上移动对象。 例如,如果在第一次旋转(新角度为 45°)之后我从右到左触摸屏幕,则对象将遵循 45° 对角线路径而不是我绘制的路径。
期望:我希望我的对象完全按照我在屏幕上创建的路径而不管其旋转。
情况:我通过代码在 Canvas 中放置了一个 Rectangle,该矩形具有“IsManipulationEnabled = true”和“RenderTransformOrigin = new Point(.5, .5)”和“mySuperRectangle.ManipulationDelta += Container_ManipulationDelta;”
这是我使用的代码:
private void Container_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
try
{
if (e.OriginalSource != null)
{
Rectangle image = e.OriginalSource as Rectangle;
Point center = new Point(image.ActualWidth / 2.0, image.ActualHeight / 2.0);
Matrix imageMatrix = ((MatrixTransform)image.RenderTransform).Matrix;
center = imageMatrix.Transform(center);
// Move the Rectangle.
imageMatrix.Translate(e.DeltaManipulation.Translation.X,
e.DeltaManipulation.Translation.Y);
// Rotate the Rectangle.
imageMatrix.RotateAt(e.DeltaManipulation.Rotation,
center.X,
center.Y);
// Resize the Rectangle. Keep it square
// so use only the X value of Scale.
imageMatrix.ScaleAt(e.DeltaManipulation.Scale.X,
e.DeltaManipulation.Scale.X,
center.X,
center.Y);
// Apply changes
image.RenderTransform = new MatrixTransform(imageMatrix);
Rect containingRect =
new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);
Rect shapeBounds =
image.RenderTransform.TransformBounds(
new Rect(image.RenderSize));
// Check if the rectangle is completely in the window.
// If it is not and intertia is occuring, stop the manipulation.
if (e.IsInertial && !containingRect.Contains(shapeBounds))
{
e.Complete();
}
e.Handled = true;
}
}
catch (Exception)
{
throw;
}
}
有什么想法吗?
非常感谢。
【问题讨论】:
标签: c# wpf manipulationdelta