-1
我有一個自定義的用戶控件。其中一部分是指示選擇的矩形。我想在用戶繪製它之後爲其設置動畫。我有以下幾種方法在WPF中同一對象上的兩個動畫
private AnimationTimeline StartRectangleHeightAnimation(Rectangle rectangle)
{
return new DoubleAnimation(
SUB_SELECTION_HEIGHT_RATIO * selectionCanvas.RenderSize.Height,
TimeSpan.FromMilliseconds(500));
}
爲矩形高度和
private AnimationTimeline StartRectangleMarginAnimation(Rectangle rectangle)
{
Thickness t = new Thickness(rectangle.Margin.Left,
(selectionCanvas.RenderSize.Height - rectangle.Height)/2,
rectangle.Margin.Right, rectangle.Margin.Bottom);
ThicknessAnimation animation = new ThicknessAnimation(t, TimeSpan.FromMilliseconds(500));
animation.EasingFunction = new ExponentialEase()
{
EasingMode = EasingMode.EaseOut,
Exponent = -5
};
animation.Completed += (s, e) =>
{
// DO STUFF.
};
return animation;
}
現在,在自己的每一個動畫的工作,我可以做
var animation = StartRectangleMarginAnimation(rectangle);
rectangle.BeginAnimation(Rectangle.MarginProperty, animation);
或
var animation = StartRectangleHeightAnimation(rectangle);
rectangle.BeginAnimation(Rectangle.HeightProperty, animation);
但不是兩者都有。據我所知,一個動畫「覆蓋」另一個。所以我們需要一個Storyboard
,所以。我現在有
AnimationTimeline shrink = StartRectangleHeightAnimation(rectangle);
AnimationTimeline move = StartRectangleMarginAnimation(rectangle);
Storyboard.SetTarget(shrink, rectangle);
Storyboard.SetTargetProperty(shrink, new PropertyPath(Rectangle.HeightProperty));
Storyboard.SetTarget(move, rectangle);
Storyboard.SetTargetProperty(move, new PropertyPath(Rectangle.MarginProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(shrink);
storyboard.Children.Add(move);
storyboard.Begin(this);
但是,這個動畫高度,但不是邊距。我在這裏做錯了什麼?
您的代碼正常工作,請確保StartRectangleMarginAnimation中的厚度有效(無負值)。請注意,您的緩動功能並不平滑(指數爲-5),因此將兩個動畫的速度降低到5秒(從0.5開始),您會看到它如何進入更接近尾聲的狀態(因爲指數易於使用大的負指數) – Evk