2011-08-09 29 views
1

我認爲類可以簡化您很多時間,並且代碼變得更有條理。我正在創建一個類,以便用代碼爲對象設置動畫。我創建的類使您可以使用主要動畫。如果我想動畫基於該類的對象,我會做:無法在背後的代碼上同時動畫兩個動畫

// Instantiate an object from animatinos class 
MyStoryboards.Animations a = new MyStoryboards.Animations(this); 


// add animations to storybaord 
a.addAnimation(brdBorder, MyStoryboards.Animations.animProperty.TranslateX, 150, TimeSpan.FromSeconds(4)); 
a.addAnimation(brdBorder, MyStoryboards.Animations.animProperty.Rotate, 150, TimeSpan.FromSeconds(4)); 
a.addAnimation(brdBorder, MyStoryboards.Animations.animProperty.Opacity, 0, TimeSpan.FromSeconds(4),1,TimeSpan.FromSeconds(1), a.getBackEase()); 

// start animations 
a.beginAnimation(); 

// delete all animations 
a.removeAllAnimations(); 

,我當我執行該對象旋轉,它的不透明度動畫播放至。但它的翻譯沒有動畫。如果我擺脫:

a.addAnimation(brdBorder, MyStoryboards.Animations.animProperty.Rotate, 150, TimeSpan.FromSeconds(4)); 

那麼對象brdBorder將轉化allong x軸和它的不透明度也會被動畫。我剛剛完成這個課程的學習,我似乎無法弄清楚什麼時候我能夠同時動畫幾個東西,什麼時候我也無法動畫。大多數情況下,我可以製作兩個或兩個以上的動畫。

讓我來提供這個類的代碼,我認爲通過一些修改它可以非常有幫助。它的功能在於可以爲多個屬性設置動畫效果,但它在功能上並非功能強大,有時候您可能想同時爲兩件事物製作動畫。無論如何是這裏的代碼:

using System; 
using System.Collections.Generic; 
using System.Windows.Media.Animation; 
using System.Windows; 
using System.Windows.Media; 

namespace MyStoryboards 
{ 

    /// <summary> 
    /// Main animations. 
    /// </summary> 
    class Animations 
    {   
     private List<object> lstStoryboardsToAnimate; 
     private List<string> lstRegisterNames; 
     private Window window; 

     /// <summary> 
     /// Constructor 
     /// </summary> 
     /// <param name="window">window where animations will take place. USE THE this keyword</param> 
     public Animations(Window window) 
     { 
      this.window = window; 
      lstStoryboardsToAnimate = new List<object>(); 
      lstRegisterNames = new List<string>(); 
     } 


     public enum animProperty 
     { 
      Height, 
      Width, 
      Opacity, 
      Margin, 
      TranslateX, 
      TranslateY, 
      Rotate, 
      ScaleX, 
      ScaleY, 
      SkeyX, 
      SkeyY, 
      CenterPoint 
     } 
     public void addAnimation(DependencyObject target, animProperty property, dynamic to, TimeSpan duration, dynamic from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
     { 
      switch (property) 
      { 
       case animProperty.Height: 
        lstStoryboardsToAnimate.Add(
         animDouble(target, FrameworkElement.HeightProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.Width: 
        lstStoryboardsToAnimate.Add(
         animDouble(target, FrameworkElement.WidthProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.Opacity: 
        lstStoryboardsToAnimate.Add(
         animDouble(target, FrameworkElement.OpacityProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.Margin: 
        lstStoryboardsToAnimate.Add(
         animThickness(target, FrameworkElement.MarginProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.TranslateX: 
        lstStoryboardsToAnimate.Add(
         animTranslateTransform(target, TranslateTransform.XProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.TranslateY: 
        lstStoryboardsToAnimate.Add(
         animTranslateTransform(target, TranslateTransform.YProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.Rotate: 
        lstStoryboardsToAnimate.Add(
         animRotateTransform(target, RotateTransform.AngleProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.ScaleX: 
        lstStoryboardsToAnimate.Add(
         animScaleTransfrom(target, ScaleTransform.ScaleXProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.ScaleY: 
        lstStoryboardsToAnimate.Add(
         animScaleTransfrom(target, ScaleTransform.ScaleYProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.SkeyX: 
        lstStoryboardsToAnimate.Add(
         animSkeyTransform(target, SkewTransform.AngleXProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.SkeyY: 
        lstStoryboardsToAnimate.Add(
         animSkeyTransform(target, SkewTransform.AngleYProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       case animProperty.CenterPoint: 
        lstStoryboardsToAnimate.Add(
         animCenterTransform(target, FrameworkElement.RenderTransformOriginProperty, to, duration, from, beginTime, e) 
        ); 
        break; 
       default: 
        break; 
      } 
     } 
     public void beginAnimation() 
     { 
      Storyboard sb = new Storyboard(); 
      foreach(var temp in lstStoryboardsToAnimate) 
      { 
       if(temp is DoubleAnimation) 
        sb.Children.Add((DoubleAnimation)temp); 
       else if (temp is ThicknessAnimation) 
        sb.Children.Add((ThicknessAnimation)temp); 
       else if (temp is PointAnimation) 
        sb.Children.Add((PointAnimation)temp); 
      } 

      sb.Begin(window); 


     } 
     public void removeAllAnimations() 
     { 
      lstRegisterNames = new List<string>(); 
      lstStoryboardsToAnimate = new List<object>(); 
     } 


     //test method 
     private void test(dynamic brdBorder) 
     { 
      TranslateTransform animatedTranslateTransform = new TranslateTransform(); 

      try 
      { 
       brdBorder.RenderTransform = animatedTranslateTransform; 
      } 
      catch 
      { 
       MessageBox.Show(brdBorder.ToString() + " cannot be animated. "); 
       return; 
      } 

      window.RegisterName("AnimatedTranslateTransform", animatedTranslateTransform); 


      DoubleAnimationUsingKeyFrames translationAnimation = new DoubleAnimationUsingKeyFrames(); 
      translationAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(100, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)))); 



      //DoubleAnimation dblaHeight = new DoubleAnimation(); 
      //dblaHeight.BeginTime = TimeSpan.FromSeconds(0); 
      //dblaHeight.Duration = TimeSpan.FromSeconds(1); 
      //dblaHeight.To = 48; 



      Storyboard.SetTargetName(translationAnimation, "AnimatedTranslateTransform"); 
      Storyboard.SetTargetProperty(translationAnimation, new PropertyPath(TranslateTransform.XProperty)); 
      //Storyboard.SetTargetName(dblaHeight, brdBorder.Name); 
      //Storyboard.SetTargetProperty(dblaHeight, new PropertyPath(Border.HeightProperty)); 

      Storyboard strbStoryboard = new Storyboard(); 
      strbStoryboard.Children.Add(translationAnimation); 
      //strbStoryboard.Children.Add(dblaHeight); 

      strbStoryboard.Begin(window); 
      window.UnregisterName("AnimatedTranslateTransform"); 

     } 

     #region doubleAnimations 
     private DoubleAnimation createDoubleAnimation(double to, TimeSpan duration, dynamic from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
     { 
      DoubleAnimation animation = new DoubleAnimation(); 
      if (from != null) 
       animation.From = (double)from; 

      animation.To = to; 

      animation.Duration = duration; 

      if (e != null) 
       animation.EasingFunction = e; 

      if (beginTime != null) 
       animation.BeginTime = beginTime; 

      return animation; 
     } 
     private string registerRandName() 
     { 
      Random r = new Random(); 
      string registerName = "animation" + r.Next().ToString(); 
      lstRegisterNames.Add(registerName); 
      return registerName; 
     } 
     private DoubleAnimation animDouble(DependencyObject target, DependencyProperty property, double to, TimeSpan duration, double? from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
     { 
      DoubleAnimation animation = createDoubleAnimation(to, duration, from, beginTime, e); 
      Storyboard.SetTarget(animation, target); // what object will be animated? 
      Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated 

      return animation; 
      //sb.Begin(); 
     } 
     private DoubleAnimation animTranslateTransform(dynamic target, DependencyProperty property, double to, TimeSpan duration, double? from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
     { 
      string registerName = this.registerRandName(); 
      TranslateTransform animatedTranslateTransform = new TranslateTransform(); 
      try{target.RenderTransform = animatedTranslateTransform;} catch { MessageBox.Show(target.ToString() + " cannot be animated. "); 
      throw new NotImplementedException(); 
      } 

      window.RegisterName(registerName, animatedTranslateTransform); 
      DoubleAnimation doubleAnimation = createDoubleAnimation(to,duration,from,beginTime,e); 
      Storyboard.SetTargetName(doubleAnimation, registerName); 
      Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(property)); 
      return doubleAnimation; 
     } 
     private DoubleAnimation animRotateTransform(dynamic target, DependencyProperty property, double to, TimeSpan duration, double? from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
     { 
      string registerName = this.registerRandName(); 

      RotateTransform animatedRotateTransform = new RotateTransform(); 
      try { target.RenderTransform = animatedRotateTransform; } 
      catch 
      { 
       MessageBox.Show(target.ToString() + " cannot be animated. "); 
       throw new NotImplementedException(); 
      } 

      window.RegisterName(registerName, animatedRotateTransform); 

      DoubleAnimation translationAnimation = createDoubleAnimation(to, duration, from, beginTime, e); 
      Storyboard.SetTargetName(translationAnimation, registerName); 
      Storyboard.SetTargetProperty(translationAnimation, new PropertyPath(property)); 
      return translationAnimation; 
     } 
     private DoubleAnimation animScaleTransfrom(dynamic target, DependencyProperty property, double to, TimeSpan duration, double? from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
     { 
      string registerName = this.registerRandName(); 
      ScaleTransform animateScaleTransform = new ScaleTransform(); 
      try { target.RenderTransform = animateScaleTransform; } 
      catch 
      { 
       MessageBox.Show(target.ToString() + " cannot be animated. "); 
       throw new NotImplementedException(); 
      } 

      window.RegisterName(registerName, animateScaleTransform); 

      DoubleAnimation translationAnimation = createDoubleAnimation(to, duration, from, beginTime, e); 
      Storyboard.SetTargetName(translationAnimation, registerName); 
      Storyboard.SetTargetProperty(translationAnimation, new PropertyPath(property)); 
      return translationAnimation; 
     } 
     private DoubleAnimation animSkeyTransform(dynamic target, DependencyProperty property, double to, TimeSpan duration, double? from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
     { 
      string registerName = this.registerRandName(); 
      SkewTransform animateScaleTransform = new SkewTransform(); 
      try { target.RenderTransform = animateScaleTransform; } 
      catch 
      { 
       MessageBox.Show(target.ToString() + " cannot be animated. "); 
       throw new NotImplementedException(); 
      } 
      window.RegisterName(registerName, animateScaleTransform); 
      DoubleAnimation translationAnimation = createDoubleAnimation(to, duration, from, beginTime, e); 
      Storyboard.SetTargetName(translationAnimation, registerName); 
      Storyboard.SetTargetProperty(translationAnimation, new PropertyPath(property)); 
      return translationAnimation; 
     } 
     #endregion 


     private PointAnimation animCenterTransform(DependencyObject target, DependencyProperty property, Point to, TimeSpan duration, Point? from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
     { 
      PointAnimation animation = new PointAnimation(); 
      animation.To = to; 

      if (beginTime == null) 
       beginTime = TimeSpan.FromSeconds(0); 

      if (from != null) 
       animation.From = from; 


      animation.BeginTime = beginTime; 
      animation.Duration = duration; 


      if (e != null) 
       animation.EasingFunction = e; 

      //start animating 
      Storyboard.SetTarget(animation, target); // what object will be animated? 
      Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated 

      return animation; 

     } 
     private ThicknessAnimation animThickness(DependencyObject target, DependencyProperty property, Thickness to, TimeSpan duration, Thickness? from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
     { 
      ThicknessAnimation animation = new ThicknessAnimation(); 
      animation.To = to; 

      if (beginTime == null) 
       beginTime = TimeSpan.FromSeconds(0); 

      if (from != null) 
       animation.From = from; 


      animation.BeginTime = beginTime; 
      animation.Duration = duration; 


      if (e != null) 
       animation.EasingFunction = e; 

      //start animating 
      Storyboard.SetTarget(animation, target); // what object will be animated? 
      Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated 

      return animation; 

     } 

     #region EaseGetters //accessor methods 
     public BackEase getBackEase(EasingMode easingMode = EasingMode.EaseIn, double amplitude = 1) 
     { 
      var r = new BackEase(); 
      r.Amplitude = (Double)amplitude; 
      r.EasingMode = easingMode; 
      return r; 
     } 

     public BounceEase getBounceEase(EasingMode easingMode = EasingMode.EaseIn, int bounces = 3, double bounciness = 2) 
     { 
      var r = new BounceEase(); 
      r.Bounces = bounces; 
      r.Bounciness = bounciness; 
      return r; 
     } 

     public CircleEase getCircleEase(EasingMode easingMode = EasingMode.EaseIn) 
     { 
      var r = new CircleEase(); 
      return r; 
     } 

     public CubicEase getCubicEase(EasingMode easingMode = EasingMode.EaseIn) 
     { 
      var r = new CubicEase(); 
      return r; 
     } 

     public ElasticEase getElasticEase(EasingMode easingMode = EasingMode.EaseIn, int oscillations = 3, double springiness = 3) 
     { 
      var r = new ElasticEase(); 
      r.Oscillations = oscillations; 
      r.Springiness = springiness; 
      return r; 
     } 

     public ExponentialEase getCircleEase(EasingMode easingMode = EasingMode.EaseIn, double exponent = 2) 
     { 
      var r = new ExponentialEase(); 
      r.Exponent = exponent; 
      return r; 
     } 

     public PowerEase getPowerEase(EasingMode easingMode = EasingMode.EaseIn, double power = 2) 
     { 
      var r = new PowerEase(); 
      r.Power = power; 
      return r; 
     } 

     public QuadraticEase getQuadraticEase(EasingMode easingMode = EasingMode.EaseIn) 
     { 
      var r = new QuadraticEase(); 
      return r; 
     } 

     public QuinticEase getQuinticEase(EasingMode easingMode = EasingMode.EaseIn) 
     { 
      var r = new QuinticEase(); 
      return r; 
     } 

     public SineEase getSineEase(EasingMode easingMode = EasingMode.EaseIn) 
     { 
      var r = new SineEase(); 
      return r; 
     } 

     #endregion 



    } 
} 

我認爲這個類可以節省大量的時間,是非常有用的。試一試。或者,也許我必須在單獨的線程中運行這些方法,以便同時對幾件事物進行動畫處理?

回答

2

爲什麼它不能同時工作?很簡單,如果你選擇一個操作作爲目標屬性的類將覆蓋被保存在目標的RenderTransform完全不考慮任何現有的轉換,所以如果你想翻譯的操作將被改變爲現有的操作,如果你開始輪換,翻譯將會消失。

這個班有幾個問題,我不會推薦使用它。 :)