如何在UIElement上执行变换动画?
以下代码通过动画移动矩形,但在运行动画时不会对其进行转换。
我已经查看了教程,并且对于为什么以下XAML不起作用我完全感到困惑:
XAML:
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Canvas.Top)"
From="20" To="73"
Duration="0:0:.15">
</DoubleAnimation>-->
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(SkewTransform.AngleX)"
From="0" To="-25"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(RotateTransform.Angle)"
From="0" To="-25"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TranslateTransform.X)"
From="0" To="-40"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TranslateTransform.Y)"
From="0" To="20"
Duration="0:0:.15">
</DoubleAnimation>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Canvas>
<Rectangle x:Name="FrontRectangle" Width="400" Height="246" Fill="Purple"
Canvas.Left="285" Canvas.Top="20" Panel.ZIndex="4" />
<Rectangle x:Name="RightRectangle" Width="100" Height="100" Fill="LightBlue"
RenderTransformOrigin="0.5,0.5" Canvas.Left="774" Canvas.Top="191" Panel.ZIndex="2">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="1" ScaleX="2.5"/>
<SkewTransform AngleX="60"/>
<RotateTransform Angle="-145"/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="LeftRectangle" Width="100" Height="100" Fill="LightGreen"
RenderTransformOrigin="0.5,0.5" Canvas.Left="99" Canvas.Top="191" Panel.ZIndex="2" >
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="1" ScaleX="2.5"/>
<SkewTransform AngleX="-60"/>
<RotateTransform Angle="145"/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="MidLeftRectangle" Width="350" Height="160" Fill="LightSeaGreen"
Canvas.Left="159" Canvas.Top="73" RenderTransformOrigin="0.5,0.5" Panel.ZIndex="3" >
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleX="-25"/>
<RotateTransform Angle="-25"/>
<TranslateTransform X="-40" Y="20"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="MidRightRectangle" Width="350" Height="160" Fill="LightCoral"
Canvas.Left="559" Canvas.Top="73" RenderTransformOrigin="0.5,0.5" Panel.ZIndex="3" >
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleX="25"/>
<RotateTransform Angle="25"/>
<TranslateTransform X="-40" Y="20"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</Grid>
代码隐藏:
private void OnSwipedLeft()
{
var visualState = "SwipeLeftState";
var success = VisualStateManager.GoToElementState(MyGrid, visualState, true);
Debug.Assert(success);
}
答案 0 :(得分:2)
我必须插入&#34;(TransformGroup.Children)[some_index]&#34;在RenderTransform和实际的转换类型之间。
<Grid x:Name="MyGrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SwipeLeftGroup">
<VisualState x:Name="SwipeLeftState">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Canvas.Left)"
From="285" To="159"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Canvas.Top)"
From="20" To="73"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)"
From="0" To="-25"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
From="0" To="-25"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
From="0" To="-40"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"
From="0" To="20"
Duration="0:0:.15">
</DoubleAnimation>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Canvas>
<Rectangle x:Name="FrontRectangle" Width="400" Height="246" Fill="Purple"
Canvas.Left="290" Canvas.Top="20" Panel.ZIndex="4">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="RightRectangle" Width="100" Height="100" Fill="LightBlue"
RenderTransformOrigin="0.5,0.5" Canvas.Left="774" Canvas.Top="191" Panel.ZIndex="2">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="1" ScaleX="2.5"/>
<SkewTransform AngleX="60"/>
<RotateTransform Angle="-145"/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="LeftRectangle" Width="100" Height="100" Fill="LightGreen"
RenderTransformOrigin="0.5,0.5" Canvas.Left="99" Canvas.Top="191" Panel.ZIndex="2" >
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="1" ScaleX="2.5"/>
<SkewTransform AngleX="-60"/>
<RotateTransform Angle="145"/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="MidLeftRectangle" Width="350" Height="160" Fill="LightSeaGreen"
Canvas.Left="159" Canvas.Top="73" RenderTransformOrigin="0.5,0.5" Panel.ZIndex="3" >
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleX="-25"/>
<RotateTransform Angle="-25"/>
<TranslateTransform X="-40" Y="20"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="MidRightRectangle" Width="350" Height="160" Fill="LightCoral"
Canvas.Left="559" Canvas.Top="73" RenderTransformOrigin="0.5,0.5" Panel.ZIndex="3" >
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleX="25"/>
<RotateTransform Angle="25"/>
<TranslateTransform X="-40" Y="20"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</Grid>