AnimationHelper.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Animation;
  9. namespace VirgoUI.Controls.Common
  10. {
  11. public class AnimationHelper
  12. {
  13. public static void TranslateX(TranslateTransform translation, int targetX, int ms, Action actionComplete = null)
  14. {
  15. var time = TimeSpan.FromMilliseconds(ms);
  16. var oldValue = translation.X;
  17. var animation = new DoubleAnimation(oldValue, targetX, time);
  18. animation.Completed += (s, e) =>
  19. {
  20. actionComplete?.Invoke();
  21. };
  22. translation.BeginAnimation(TranslateTransform.XProperty, animation);
  23. }
  24. public static void TranslateX(UIElement uIElement, int startX, int targetX, int ms, Action actionComplete = null)
  25. {
  26. var transform = new TranslateTransform();
  27. uIElement.RenderTransform = transform;
  28. var time = TimeSpan.FromMilliseconds(ms);
  29. var animation = new DoubleAnimation(startX, targetX, time);
  30. animation.AutoReverse = false;
  31. animation.Completed += (s, e) =>
  32. {
  33. actionComplete?.Invoke();
  34. };
  35. transform.BeginAnimation(TranslateTransform.XProperty, animation);
  36. }
  37. public static void TranslateY(TranslateTransform translation, int targetY, int ms, Action actionComplete = null)
  38. {
  39. var time = TimeSpan.FromMilliseconds(ms);
  40. var animation = new DoubleAnimation(targetY, time);
  41. animation.Completed += (s, e) =>
  42. {
  43. actionComplete?.Invoke();
  44. };
  45. translation.BeginAnimation(TranslateTransform.YProperty, animation);
  46. }
  47. public static void TranslateYRelative(TranslateTransform translation, int y, int ms, Action actionComplete = null)
  48. {
  49. var currentY = translation.Y;
  50. var time = TimeSpan.FromMilliseconds(ms);
  51. var animation = new DoubleAnimation(currentY + y, time);
  52. animation.Completed += (s, e) =>
  53. {
  54. actionComplete?.Invoke();
  55. };
  56. translation.BeginAnimation(TranslateTransform.YProperty, animation);
  57. }
  58. }
  59. }