AnimationHelper.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 FurnaceUI.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(UIElement uIElement, int startY, int targetY, int ms, Action actionComplete = null)
  38. {
  39. var transform = new TranslateTransform();
  40. uIElement.RenderTransform = transform;
  41. var time = TimeSpan.FromMilliseconds(ms);
  42. var animation = new DoubleAnimation(startY, targetY, time);
  43. animation.AutoReverse = false;
  44. animation.Completed += (s, e) =>
  45. {
  46. actionComplete?.Invoke();
  47. };
  48. transform.BeginAnimation(TranslateTransform.YProperty, animation);
  49. }
  50. public static void TranslateY(TranslateTransform translation, int targetY, int ms, Action actionComplete = null)
  51. {
  52. var time = TimeSpan.FromMilliseconds(ms);
  53. var animation = new DoubleAnimation(targetY, time);
  54. animation.Completed += (s, e) =>
  55. {
  56. actionComplete?.Invoke();
  57. };
  58. translation.BeginAnimation(TranslateTransform.YProperty, animation);
  59. }
  60. public static void TranslateYRelative(TranslateTransform translation, int y, int ms, Action actionComplete = null)
  61. {
  62. var currentY = translation.Y;
  63. var time = TimeSpan.FromMilliseconds(ms);
  64. var animation = new DoubleAnimation(currentY + y, time);
  65. animation.Completed += (s, e) =>
  66. {
  67. actionComplete?.Invoke();
  68. };
  69. translation.BeginAnimation(TranslateTransform.YProperty, animation);
  70. }
  71. }
  72. }