不能在同一时间上code动画两个动画背后 [英] Not able to animate two animations at the same time on code behind

查看:210
本文介绍了不能在同一时间上code动画两个动画背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为类简化您大量的时间加上code变得更有条理。我以动画与code对象创建类。那我创建的类,您可以使用主动画。如果我想基于该类我会做动画的对象:

I think that classes simplify you a lot of time plus the code becomes more organized. I am creating a class in order to animate objects with code. The class that I am creating enables you to use the main animations. If I want to animate an object based on that class I would do:

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

当我执行该对象的旋转和它的不透明度动画到我。但它的翻译没有动画。如果我摆脱:

and I when I execute that the object rotates and it's opacity animates to. but it's translation does not animate. if I get rid of :

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

那么对象brdBorder将翻译allong x轴和它的不透明度也将被动画。我只是完成建设这个班,我似乎无法弄清楚的时候我能够在同一时间以动画几件事情,当我不能够。大多数时候,我能够动画两点或更多。

then the object brdBorder will translate allong the x axis and it's opacity will be animated as well. I just finish building this class and I cannot seem to figure out when I am able to animate several things at the same time and when I am not able to. Most of the time I am able to animate two things or more.

让我提供code的类我认为有一些修改它可以是非常有益的。它是在你可以几个动画属性的方式的功能,但它不是,有时你可能需要在同一时间以动画两件事SENCE功能。反正这里是code:

Let me provide the code for the class I think that with some modifications it can be very helpfull. it is functional in the way that you can animate several properties but it is not functional in the sence that sometimes you may want to animate two things at the same time. Anyways here is the code:

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



    }
}

我认为类可以节省大量的时间,是非常有用的。试一试。或者,也许我必须运行在单独的线程的方法,以便在同一时间以动画几件事情?

I think that class can save a lot of time and be very useful. give it a try. Or maybe I have to run the methods in separate threads in order to animate several things at the same time?

推荐答案

为什么它不能同时工作吗?很简单,如果你选择一个操作作为目标属性的类会的覆盖的现有操作保存在目标的的RenderTransform 完全不考虑任何现有的转换的,所以如果你想翻译的操作将改为,如果你然后开始旋转平移将不复存在。

Why does it not work simultaneously? Simple, if you choose a manipulation as the target property the class will overwrite the existing manipulation that is saved in the target's RenderTransform, completely disregarding any existing transforms, so if you want a translation the manipulation will be changed to that, if you then start a rotation the translation will be gone.

本类有几个问题,我不建议使用它。 :)

This class has a few issues, i would not recommend using it. :)

这篇关于不能在同一时间上code动画两个动画背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆