【发布时间】:2018-01-11 02:59:05
【问题描述】:
我对 UWP 中的动画有疑问。我想在我的应用程序底部有一个菜单,当点击顶部时,它会向上滑动(显示)或向下滑动(几乎完全隐藏)。我之前在学习 WPF,在那里我知道我可以使用 ThicknessAnimation 来移动我的控件的边距并让它滑动。不幸的是,在 UWP 中我不能使用 ThicknessAnimations,所以我试图找到另一种方法。我希望这适用于任意 FrameworkElement(以便能够重用它)。最终,我想出了这个解决方案:
/// <summary>
/// Adds a vertical slide animation
/// </summary>
/// <param name="storyboard">The storyboard to add the animation to</param>
/// <param name="seconds">The time the animation will take</param>
/// <param name="offset">The distance the element will cover (nagative is up, positive is down)</param>
public static void AddVerticalSlide(this Storyboard storyboard, FrameworkElement element, float seconds, double offset)
{
var slideAnimation = new ObjectAnimationUsingKeyFrames();
for (int i = 0; i <= 100; ++i)
{
double scalar = (double)i / 100;
slideAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame
{
Value = new Thickness(0, scalar*offset, 0, -scalar*offset),
KeyTime = TimeSpan.FromSeconds(scalar*seconds),
});
}
//slideAnimation.Duration = TimeSpan.FromSeconds(seconds);
// Set the target and target property
Storyboard.SetTarget(slideAnimation, element);
Storyboard.SetTargetProperty(slideAnimation, "(FrameworkElement.Margin)");
// Add the animation to the storyboard
storyboard.Children.Add(slideAnimation);
}
它有效,看起来不错,但这就是我问这个问题的原因:我不知道它是否正确。我的猜测是,有一种比手动定义 100 个点并使用此动画将对象移动到每个点更好的方法来滑动对象。
【问题讨论】:
标签: c# xaml animation uwp slide