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));
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.
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?