ControlAnimations.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Avalonia;
  2. using Avalonia.Animation;
  3. using Avalonia.Animation.Easings;
  4. using Avalonia.Controls;
  5. using Avalonia.Media;
  6. using Avalonia.Styling;
  7. using System;
  8. namespace SukiUI;
  9. public static class ControlAnimationHelper
  10. {
  11. public static void Jump(this Control control)
  12. {
  13. var currentMargin = control.Margin;
  14. new Avalonia.Animation.Animation
  15. {
  16. Duration = TimeSpan.FromMilliseconds(800),
  17. FillMode = FillMode.Forward,
  18. Easing = new QuadraticEaseOut(),
  19. IterationCount = new IterationCount(1),
  20. PlaybackDirection = PlaybackDirection.Normal,
  21. Children =
  22. {
  23. new KeyFrame()
  24. {
  25. Setters =
  26. {
  27. new Setter { Property = Control.MarginProperty, Value = new Thickness(currentMargin.Left, currentMargin.Top, currentMargin.Right, currentMargin.Bottom)},
  28. },
  29. KeyTime = TimeSpan.FromSeconds(0)
  30. },
  31. new KeyFrame()
  32. {
  33. Setters =
  34. {
  35. new Setter { Property = Control.MarginProperty, Value = new Thickness(currentMargin.Left, currentMargin.Top - 8, currentMargin.Right, currentMargin.Bottom +8)},
  36. },
  37. KeyTime = TimeSpan.FromMilliseconds(300)
  38. },
  39. new KeyFrame()
  40. {
  41. Setters =
  42. {
  43. new Setter { Property = Control.MarginProperty, Value = new Thickness(currentMargin.Left, currentMargin.Top, currentMargin.Right, currentMargin.Bottom)},
  44. },
  45. KeyTime = TimeSpan.FromMilliseconds(600)
  46. },
  47. new KeyFrame()
  48. {
  49. Setters =
  50. {
  51. new Setter { Property = Control.MarginProperty, Value = new Thickness(currentMargin.Left, currentMargin.Top-2, currentMargin.Right, currentMargin.Bottom +2)},
  52. },
  53. KeyTime = TimeSpan.FromMilliseconds(700)
  54. },new KeyFrame()
  55. {
  56. Setters =
  57. {
  58. new Setter { Property = Control.MarginProperty, Value = new Thickness(currentMargin.Left, currentMargin.Top, currentMargin.Right, currentMargin.Bottom)},
  59. },
  60. KeyTime = TimeSpan.FromMilliseconds(800)
  61. }
  62. }
  63. }.RunAsync(control);
  64. }
  65. public static void Vibrate(this Animatable control, TimeSpan duration)
  66. {
  67. var count = duration.TotalMilliseconds / 75;
  68. new Avalonia.Animation.Animation
  69. {
  70. Duration = TimeSpan.FromMilliseconds(75),
  71. FillMode = FillMode.Forward,
  72. Easing = new CubicEaseInOut(),
  73. IterationCount = new IterationCount((ulong)(count)),
  74. PlaybackDirection = PlaybackDirection.Normal,
  75. Children =
  76. {
  77. new KeyFrame()
  78. {
  79. Setters =
  80. {
  81. new Setter { Property = ScaleTransform.ScaleXProperty, Value = 0.995},
  82. new Setter { Property = ScaleTransform.ScaleYProperty, Value = 0.995},
  83. new Setter { Property = RotateTransform.AngleProperty, Value = 0.4}
  84. },
  85. KeyTime = TimeSpan.FromSeconds(0)
  86. },
  87. new KeyFrame()
  88. {
  89. Setters =
  90. {
  91. new Setter { Property = ScaleTransform.ScaleXProperty, Value = 1.005},
  92. new Setter { Property = ScaleTransform.ScaleYProperty, Value = 1.005},
  93. new Setter { Property = RotateTransform.AngleProperty, Value = -0.3}
  94. },
  95. KeyTime = TimeSpan.FromMilliseconds(75)
  96. }
  97. }
  98. }.RunAsync(control);
  99. }
  100. public static void Animate<T>(this Animatable control, AvaloniaProperty Property, T from, T to, TimeSpan duration, ulong count = 1)
  101. {
  102. new Avalonia.Animation.Animation
  103. {
  104. Duration = duration,
  105. FillMode = FillMode.Forward,
  106. Easing = new CubicEaseInOut(),
  107. IterationCount = new IterationCount(count),
  108. PlaybackDirection = PlaybackDirection.Normal,
  109. Children =
  110. {
  111. new KeyFrame()
  112. {
  113. Setters = { new Setter { Property = Property, Value = from } },
  114. KeyTime = TimeSpan.FromSeconds(0)
  115. },
  116. new KeyFrame()
  117. {
  118. Setters = { new Setter { Property = Property, Value = to } },
  119. KeyTime = duration
  120. }
  121. }
  122. }.RunAsync(control);
  123. }
  124. public static void Animate<T>(this Animatable control, AvaloniaProperty Property, T from, T to)
  125. {
  126. new Avalonia.Animation.Animation
  127. {
  128. Duration = TimeSpan.FromMilliseconds(500),
  129. FillMode = FillMode.Forward,
  130. Easing = new CubicEaseInOut(),
  131. IterationCount = new IterationCount(1),
  132. PlaybackDirection = PlaybackDirection.Normal,
  133. Children =
  134. {
  135. new KeyFrame()
  136. {
  137. Setters = { new Setter { Property = Property, Value = from } },
  138. KeyTime = TimeSpan.FromSeconds(0)
  139. },
  140. new KeyFrame()
  141. {
  142. Setters = { new Setter { Property = Property, Value = to } },
  143. KeyTime = TimeSpan.FromMilliseconds(500)
  144. }
  145. }
  146. }.RunAsync(control);
  147. }
  148. }