您可以在具有相同坐标但方向相反的两条路径之间切换。这是一个示例MatrixAnimationUsingPath
,当 UI 元素加载时动画方向为 cw,当鼠标指针进入蓝色矩形时,动画方向变为 ccw。
<Canvas ClipToBounds="False" Width="400" Height="400">
<Path StrokeThickness="10" StrokeDashArray="" StrokeDashCap="Flat" StrokeDashOffset="0" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeLineJoin="Miter" StrokeMiterLimit="10" Stroke="#000000">
<Path.Data>
<PathGeometry Figures="M 150,250 C 150,120 300,120 300,250 C 300,390 150,390 150,250 Z" />
</Path.Data>
</Path>
<Rectangle Fill="Blue" RenderTransformOrigin="0.5,0.5" Width="10" Height="10" Margin="-5">
<Rectangle.RenderTransform>
<TransformGroup>
<MatrixTransform x:Name="tt">
<MatrixTransform.Matrix>
<Matrix />
</MatrixTransform.Matrix>
</MatrixTransform>
</TransformGroup>
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard>
<MatrixAnimationUsingPath Duration="0:0:05" Storyboard.TargetName="tt"
Storyboard.TargetProperty="Matrix"
AutoReverse="False"
DoesRotateWithTangent="True"
RepeatBehavior="Forever"
>
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 150,250 C 150,120 300,120 300,250 C 300,390 150,390 150,250 Z">
</PathGeometry>
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="FrameworkElement.MouseEnter">
<BeginStoryboard>
<Storyboard>
<MatrixAnimationUsingPath Duration="0:0:05" Storyboard.TargetName="tt"
Storyboard.TargetProperty="Matrix"
AutoReverse="False"
DoesRotateWithTangent="True"
RepeatBehavior="Forever"
>
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 150,250 C 150,390 300,390 300,250 C 300,120 150,120 150,250 Z" >
</PathGeometry>
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Canvas>