您可以使用相同的座標在兩條路徑之間切換,但是反向。這裏是一個使用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>
感謝您的建議。看起來關鍵是有兩條獨立的路徑 - 一條用於順時針方向,另一條用於逆時針方向。我希望找到一個解決方案,允許動畫使用單一路徑 - 或者,至少以編程方式操縱路徑以動態創建順時針和逆時針替代方案。 – John