WPF 抖动动画
/// <summary> /// 控件抖动 /// </summary> /// <param name="translate"></param> /// <param name="power">抖动第一下偏移量</param> /// <param name="range">减弱幅度(小于等于power,大于0)</param> /// <param name="speed">持续系数(大于0),越大时间越长,</param> public void ElasticAnimation(TranslateTransform translate, int power, int range = 1, double speed = 1) { DoubleAnimationUsingKeyFrames animation1 = new DoubleAnimationUsingKeyFrames(); for (int i = power, j = 1; i >= 0; i -= range) { animation1.KeyFrames.Add(new LinearDoubleKeyFrame(-i, TimeSpan.FromMilliseconds(j++ * 100 * speed))); animation1.KeyFrames.Add(new LinearDoubleKeyFrame(i, TimeSpan.FromMilliseconds(j++ * 100 * speed))); } translate.BeginAnimation(TranslateTransform.YProperty, animation1); DoubleAnimationUsingKeyFrames animation2 = new DoubleAnimationUsingKeyFrames(); for (int i = power, j = 1; i >= 0; i -= range) { animation2.KeyFrames.Add(new LinearDoubleKeyFrame(-i, TimeSpan.FromMilliseconds(j++ * 100 * speed))); animation2.KeyFrames.Add(new LinearDoubleKeyFrame(i, TimeSpan.FromMilliseconds(j++ * 100 * speed))); } translate.BeginAnimation(TranslateTransform.XProperty, animation2); }