不能在代码背后同时动画两个动画

本文关键字:动画 两个 代码 背后 不能 | 更新日期: 2023-09-27 18:05:14

我认为类简化了很多时间,而且代码变得更有条理。我正在创建一个类,以便用代码动画对象。我正在创建的类使您能够使用主动画。如果我想让一个对象基于这个类动画化,我会输入:

// 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();

和I,当我执行时,对象旋转,它的不透明度动画。但它的翻译并不是动画。如果我去掉:

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

则对象brdBorder将沿着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

    }
}

我认为这门课可以节省很多时间,而且非常有用。试试吧。或者我可能需要在单独的线程中运行这些方法以便同时为几个东西动画?

不能在代码背后同时动画两个动画

为什么不能同时工作?很简单,如果你选择一个操作作为目标属性,类将覆盖保存在目标的RenderTransform中的现有操作,完全忽略任何现有的转换,所以如果你想要一个转换操作将被改变,如果你然后开始旋转,转换将消失。

这个类有一些问题,我不建议使用它。:)