public static void Move(FrameworkElement target, Thickness from, Thickness to, double durationMilliseconds, EasingMode easingMode, Action callBack) { EasingFunctionBase easeFunction = new PowerEase() { EasingMode = easingMode, Power = EasingPower }; ThicknessAnimation animation = new ThicknessAnimation() { From = from, //起始值 To = to, //结束值 EasingFunction = easeFunction, //缓动函数 Duration = TimeSpan.FromMilliseconds(durationMilliseconds) //动画播放时间 }; if (callBack != null) { animation.Completed += (sender, e) => { callBack(); }; } AnimationClock clock = animation.CreateClock(); target.ApplyAnimationClock(FrameworkElement.MarginProperty, clock); }
private static void IsPressedChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs args) { CurrentRequestsPanel panel = (CurrentRequestsPanel)depObj; if (!panel.IsPressed && panel.Parent != null) { animate = new ThicknessAnimation() { From = animationClock != null ? (Thickness)animate.GetCurrentValue(animate.From, animate.To, animationClock) : new Thickness(0, 0, 0, 0), To = new Thickness(-350, 0, 0, 0), Duration = TimeSpan.FromSeconds(0.8) }; animationClock = animate.CreateClock(); animate.Completed += (o, s) => { if (!panel.IsPressed) { RemoveUserControl((Panel)LogicalTreeHelper.GetParent(panel), panel); } }; panel.BeginAnimation(MarginProperty, animate); } else if (panel.IsPressed && panel.Parent != null) { var parent = (Panel)LogicalTreeHelper.GetParent(panel); List <UserControl> userControls = parent.Children.OfType <UserControl>().ToList(); animate = new ThicknessAnimation() { From = animationClock != null ? (Thickness)animate.GetCurrentValue(animate.From, animate.To, animationClock) : userControls.Count == 1 ? new Thickness(-350, 0, 0, 0) : new Thickness(-700, 0, 0, 0), To = new Thickness(0, 0, 0, 0), Duration = TimeSpan.FromSeconds(userControls.Count > 1 ? 1 : 0.8) }; animationClock = animate.CreateClock(); animate.Completed += (o, s) => { if (userControls.Count > 1) { RemoveUserControl(parent, userControls[0]); } }; panel.BeginAnimation(MarginProperty, animate); } }
/// <summary> /// Animate the loader canvas from left to right indefinitely, /// and create its children. /// </summary> private void InitializeLoader() { for (int i = 0; i < 30; i++) { LoaderCanvas.Children.Add(new Line { X1 = -(Width + 120) + (35.0 * i), X2 = -Width + (35.0 * i) }); } ThicknessAnimation anim = new ThicknessAnimation( new Thickness(0, 0, 0, 0), new Thickness(Width + 20.0, 0, 0, 0), new Duration(TimeSpan.FromMilliseconds(4000))) { AutoReverse = false, RepeatBehavior = RepeatBehavior.Forever }; LoaderCanvas.ApplyAnimationClock(MarginProperty, anim.CreateClock()); }