2012-09-12 33 views
0

假設我有兩個動畫方法AnimateHorizo​​ntally和AnimateVertically這樣在WPF中,我怎樣才能同時使用不同的故事板動畫不同的對象?

public void AnimateHorizontally(FrameworkElement element, double XMoveStart, double XMoveEnd, int milli) 
     { 
      BackEase eEase = new BackEase(); 
      Storyboard sb = new Storyboard(); 
      DoubleAnimation daX = new DoubleAnimation(XMoveStart, XMoveEnd, new Duration(new TimeSpan(0, 0, 0, 0, milli))); 
      daX.EasingFunction = eEase; 
      Storyboard.SetTargetProperty(daX, new PropertyPath("(Canvas.Left)")); 
      sb.Children.Add(daX); 
      element.BeginStoryboard(sb); 
     } 

public void AnimateVertically(FrameworkElement element, double YMoveStart, double YMoveEnd, int milli) 
     { 
      ElasticEase eEase = new ElasticEase(); 
      Storyboard sb = new Storyboard(); 
      DoubleAnimation daY = new DoubleAnimation(YMoveStart, YMoveEnd, new Duration(new TimeSpan(0, 0, 0, 0, milli))); 
      daY.EasingFunction = eEase; 
      Storyboard.SetTargetProperty(daY, new PropertyPath("(Canvas.Top)")); 
      sb.Children.Add(daY); 
      element.BeginStoryboard(sb); 
     } 

當我應用它上的物體A,它從XMoveStart和XMoveEnd水平移動。如果我有一個對象B,並且我想讓對象B應用ElasticEase並垂直移動。

對象A和B上的動畫應該同時啓動並且流暢,我該怎麼做?

只需通過調用

AnimateVertically(A) 
AnimateHorizontally(B) 

它不工作,那光滑,似乎他們不是同時進行的。任何人都可以幫忙我不必使用Storyboard,如果有其他一些動畫方法可以完成這個工作,我也可以使用它們。

回答

0

是的,有。您只需將多個動畫放置在一個Storyboard中並設置TargetName-動畫的屬性:

這裏是一個樣例故事板。注意TargetName屬性:

<Storyboard x:Name="FromMainToBack"> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ScaleContainerGrid" Storyboard.TargetProperty="ScaleY"> 
    <LinearDoubleKeyFrame KeyTime="0:0:0.2" Value="0.8"/> 
    <LinearDoubleKeyFrame KeyTime="0:0:1.2" Value="0.8"/> 
    <LinearDoubleKeyFrame KeyTime="0:0:1.5" Value="1"/> 
    </DoubleAnimationUsingKeyFrames> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ScaleContainerGrid" Storyboard.TargetProperty="ScaleX"> 
    <LinearDoubleKeyFrame KeyTime="0:0:0.2" Value="0.8"/> 
    <LinearDoubleKeyFrame KeyTime="0:0:1.2" Value="0.8"/> 
    <LinearDoubleKeyFrame KeyTime="0:0:1.5" Value="1"/> 
    </DoubleAnimationUsingKeyFrames> 


    <DoubleAnimation BeginTime="0:0:0.2" Duration="0:0:0.5" From="0" To="90" Storyboard.TargetName="planeProjectionMain" Storyboard.TargetProperty="RotationY"> 
    <DoubleAnimation.EasingFunction> 
     <BackEase EasingMode="EaseIn"/> 
    </DoubleAnimation.EasingFunction> 
    </DoubleAnimation> 
    <DoubleAnimation BeginTime="0:0:0.7" Duration="0:0:0.5" From="270" To="360" Storyboard.TargetName="planeProjectionBack" Storyboard.TargetProperty="RotationY"> 
    <DoubleAnimation.EasingFunction> 
     <BackEase Amplitude="0" EasingMode="EaseOut"/> 
    </DoubleAnimation.EasingFunction> 
    </DoubleAnimation> 
</Storyboard> 
相關問題