Я думаю, что классы значительно упростят вам задачу, и код станет более организованным. Я создаю класс для того, чтобы анимировать объекты с кодом. Класс, который я создаю, позволяет вам использовать основные анимации. Если я хочу анимировать объект на основе этого класса, я бы сделал:
// 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 будет перемещаться по оси 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
}
}
Я думаю, этот класс может сэкономить много времени и быть очень полезным. попробуй. Или, может быть, мне придется запускать методы в отдельных потоках, чтобы оживить несколько вещей одновременно?