Rotating/scaling 围绕手势点
Rotating/scaling around point of gesture
我希望用户通过手势 moved/scaled/rotated 进行控制,并且我希望旋转和缩放的中心点位于手势的中心(例如,当使用两根手指旋转时,两者之间的点手指应该是旋转的中心)。
当我不尝试为 rotation/scaling 设置中心点或设置静态点时,一切正常。将 CompositeTransform.CenterX/Y 设置为 ManipulationDeltaRoutedEventArgs.Position 的值时,用户控件将以中心点旋转,每个手势都会错得更多,偶尔会加速。
我正在使用 CompositeTransform 作为我的用户控件的呈现转换,并且我已经连接到 ManipulationDelta 事件,如下所示:
private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
//this.transform is my composite transform
//which is set to be the render transform of the user control
this.transform.CenterX = e.Position.X;
this.transform.CenterY = e.Position.Y;
this.transform.ScaleX *= e.Delta.Scale;
this.transform.ScaleY *= e.Delta.Scale;
this.transform.Rotation += e.Delta.Rotation;
this.transform.TranslateX += e.Delta.Translation.X;
this.transform.TranslateY += e.Delta.Translation.Y;
}
似乎 e.Position 没有给我想要的东西,不幸的是文档非常简短,只说明 Gets the point from which the manipulation originated. 从我的调试打印来看,CompositeTransform.CenterX/Y和ManipulationDeltaRoutedEventArgs.Position在用户控件的坐标系中。
问题原来是CompositeTransform只能处理一个个中心点。因此,当中心点发生变化时,它也会对所有先前的转换进行追溯更改。解决方案是使用 TransformGroup 并使用自己的中心点创建单独的转换:
private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var localCoords = e.Position;
var relativeTransform = this.TransformToVisual(this.Container);
Point parentContainerCoords = relativeTransform.TransformPoint(localCoords);
var center = parentContainerCoords;
RotateTransform rotation = new RotateTransform();
rotation.CenterX = center.X;
rotation.CenterY = center.Y;
rotation.Angle = e.Delta.Rotation;
this.transformGroup.Children.Add(rotation);
ScaleTransform scaling = new ScaleTransform();
scaling.CenterX = center.X;
scaling.CenterY = center.Y;
scaling.ScaleX = e.Delta.Scale;
scaling.ScaleY = e.Delta.Scale;
this.transformGroup.Children.Add(scaling);
TranslateTransform translation = new TranslateTransform();
translation.X = e.Delta.Translation.X;
translation.Y = e.Delta.Translation.Y;
this.transformGroup.Children.Add(translation);
}
我希望用户通过手势 moved/scaled/rotated 进行控制,并且我希望旋转和缩放的中心点位于手势的中心(例如,当使用两根手指旋转时,两者之间的点手指应该是旋转的中心)。
当我不尝试为 rotation/scaling 设置中心点或设置静态点时,一切正常。将 CompositeTransform.CenterX/Y 设置为 ManipulationDeltaRoutedEventArgs.Position 的值时,用户控件将以中心点旋转,每个手势都会错得更多,偶尔会加速。
我正在使用 CompositeTransform 作为我的用户控件的呈现转换,并且我已经连接到 ManipulationDelta 事件,如下所示:
private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
//this.transform is my composite transform
//which is set to be the render transform of the user control
this.transform.CenterX = e.Position.X;
this.transform.CenterY = e.Position.Y;
this.transform.ScaleX *= e.Delta.Scale;
this.transform.ScaleY *= e.Delta.Scale;
this.transform.Rotation += e.Delta.Rotation;
this.transform.TranslateX += e.Delta.Translation.X;
this.transform.TranslateY += e.Delta.Translation.Y;
}
似乎 e.Position 没有给我想要的东西,不幸的是文档非常简短,只说明 Gets the point from which the manipulation originated. 从我的调试打印来看,CompositeTransform.CenterX/Y和ManipulationDeltaRoutedEventArgs.Position在用户控件的坐标系中。
问题原来是CompositeTransform只能处理一个个中心点。因此,当中心点发生变化时,它也会对所有先前的转换进行追溯更改。解决方案是使用 TransformGroup 并使用自己的中心点创建单独的转换:
private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var localCoords = e.Position;
var relativeTransform = this.TransformToVisual(this.Container);
Point parentContainerCoords = relativeTransform.TransformPoint(localCoords);
var center = parentContainerCoords;
RotateTransform rotation = new RotateTransform();
rotation.CenterX = center.X;
rotation.CenterY = center.Y;
rotation.Angle = e.Delta.Rotation;
this.transformGroup.Children.Add(rotation);
ScaleTransform scaling = new ScaleTransform();
scaling.CenterX = center.X;
scaling.CenterY = center.Y;
scaling.ScaleX = e.Delta.Scale;
scaling.ScaleY = e.Delta.Scale;
this.transformGroup.Children.Add(scaling);
TranslateTransform translation = new TranslateTransform();
translation.X = e.Delta.Translation.X;
translation.Y = e.Delta.Translation.Y;
this.transformGroup.Children.Add(translation);
}