如何设置SpringVector3NaturalMotionAnimation的偏移量?

时间:2019-01-06 15:22:43

标签: c# animation uwp offset

如何为offset设置SpringVector3NaturalMotionAnimation点?使用以下代码,动画效果很好,除了我无法将偏移点设置为FrameworkElement的中间。现在offset在左上角。

       private static void ScaleAnimation_Internal(FrameworkElement TargetFrameworkElement, int ScaleInProsents, TimeSpan Duration, TimeSpan Delay, int DampingRatioProsent)
        {
            AnimationClass TargetAnimationClass = AddScaleAnimation(TargetFrameworkElement);
            Compositor TargetCompositor = Window.Current.Compositor;
            //UPDATE
            TargetAnimationClass.TargetSpringVector3NaturalMotionAnimation.FinalValue = new Vector3((float)ScaleInProsents / 100);
            if (Duration != TimeSpan.Zero) TargetAnimationClass.TargetSpringVector3NaturalMotionAnimation.Period = Duration;
            if (Delay != TimeSpan.Zero) TargetAnimationClass.TargetSpringVector3NaturalMotionAnimation.DelayTime = Delay;
            if (DampingRatioProsent >= 0) TargetAnimationClass.TargetSpringVector3NaturalMotionAnimation.DampingRatio = (float)DampingRatioProsent / 100;
            //START ANIMATION
            TargetFrameworkElement.StartAnimation(TargetAnimationClass.TargetSpringVector3NaturalMotionAnimation);
        }

1 个答案:

答案 0 :(得分:1)

正如@Johnny Westlake所说,您只需要在调用CenterPoint之前设置StartAnimation。例如

Compositor _compositor = Window.Current.Compositor;
SpringVector3NaturalMotionAnimation _springAnimation;

private void Btn_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    CreateOrUpdateSpringAnimation(1.5f);
    (sender as UIElement).CenterPoint = new Vector3((float)(Btn.ActualWidth / 2.0), (float)(Btn.ActualHeight / 2.0), 1f);
    (sender as UIElement).StartAnimation(_springAnimation);

}

private void Btn_PointerExited(object sender, PointerRoutedEventArgs e)
{
    CreateOrUpdateSpringAnimation(1.0f);
    (sender as UIElement).CenterPoint = new Vector3((float)(Btn.ActualWidth / 2.0), (float)(Btn.ActualHeight / 2.0), 1f);
    (sender as UIElement).StartAnimation(_springAnimation);


}
private void CreateOrUpdateSpringAnimation(float finalValue)
{
    if (_springAnimation == null)
    {
        _springAnimation = _compositor.CreateSpringVector3Animation();
        _springAnimation.Target = "Scale";

    }

    _springAnimation.FinalValue = new Vector3(finalValue);
}

Xaml代码

<Grid>
    <Button Name="Btn" Content="ClickMe" 
            PointerEntered="Btn_PointerEntered"
            PointerExited="Btn_PointerExited"
           VerticalAlignment="Center" HorizontalAlignment="Center" >

    </Button>
</Grid>