158 lines
4.7 KiB
C#
158 lines
4.7 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Windows;
|
|
using System.Windows.Data;
|
|
using System.Windows.Media.Animation;
|
|
|
|
namespace Wpf_AiSportsMicrospace.MyUserControl
|
|
{
|
|
public class ImageStateToVisibilityConverter : IValueConverter
|
|
{
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is string state && parameter is string target)
|
|
{
|
|
return state == target ? Visibility.Visible : Visibility.Collapsed;
|
|
}
|
|
return Visibility.Collapsed;
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
public class MarginAnimationController
|
|
{
|
|
private Storyboard _storyboard;
|
|
private FrameworkElement _targetElement;
|
|
private ThicknessAnimation _thicknessAnimation;
|
|
|
|
/// <summary>
|
|
/// 初始化动画控制器
|
|
/// </summary>
|
|
/// <param name="targetElement">目标UI元素</param>
|
|
public MarginAnimationController(FrameworkElement targetElement)
|
|
{
|
|
_targetElement = targetElement;
|
|
InitializeAnimation();
|
|
}
|
|
|
|
private void InitializeAnimation()
|
|
{
|
|
// 创建厚度动画
|
|
_thicknessAnimation = new ThicknessAnimation
|
|
{
|
|
Duration = TimeSpan.FromSeconds(1),
|
|
FillBehavior = FillBehavior.HoldEnd
|
|
};
|
|
|
|
// 创建故事板
|
|
_storyboard = new Storyboard();
|
|
_storyboard.Children.Add(_thicknessAnimation);
|
|
|
|
// 设置目标属性和目标对象
|
|
Storyboard.SetTarget(_thicknessAnimation, _targetElement);
|
|
Storyboard.SetTargetProperty(_thicknessAnimation, new PropertyPath(FrameworkElement.MarginProperty));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开始动画
|
|
/// </summary>
|
|
/// <param name="targetMargin">目标边距</param>
|
|
/// <param name="durationMs">动画时长(毫秒)</param>
|
|
/// <param name="easingFunction">缓动函数</param>
|
|
public void StartAnimation(Thickness targetMargin, int durationMs = 1000, IEasingFunction easingFunction = null)
|
|
{
|
|
// 设置动画参数
|
|
_thicknessAnimation.To = targetMargin;
|
|
_thicknessAnimation.Duration = TimeSpan.FromMilliseconds(durationMs);
|
|
|
|
if (easingFunction != null)
|
|
{
|
|
_thicknessAnimation.EasingFunction = easingFunction;
|
|
}
|
|
|
|
// 开始动画
|
|
_storyboard.Begin();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开始从当前值到目标值的动画
|
|
/// </summary>
|
|
public void StartAnimationFromCurrent(Thickness targetMargin, int durationMs = 1000)
|
|
{
|
|
_thicknessAnimation.From = null; // 从当前值开始
|
|
_thicknessAnimation.To = targetMargin;
|
|
_thicknessAnimation.Duration = TimeSpan.FromMilliseconds(durationMs);
|
|
|
|
_storyboard.Begin();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开始从指定值到目标值的动画
|
|
/// </summary>
|
|
public void StartAnimationFromTo(Thickness fromMargin, Thickness toMargin, int durationMs = 1000)
|
|
{
|
|
_thicknessAnimation.From = fromMargin;
|
|
_thicknessAnimation.To = toMargin;
|
|
_thicknessAnimation.Duration = TimeSpan.FromMilliseconds(durationMs);
|
|
|
|
_storyboard.Begin();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止动画
|
|
/// </summary>
|
|
public void StopAnimation()
|
|
{
|
|
_storyboard.Stop();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 暂停动画
|
|
/// </summary>
|
|
public void PauseAnimation()
|
|
{
|
|
_storyboard.Pause();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 恢复动画
|
|
/// </summary>
|
|
public void ResumeAnimation()
|
|
{
|
|
_storyboard.Resume();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置动画完成回调
|
|
/// </summary>
|
|
public void SetCompletedCallback(EventHandler callback)
|
|
{
|
|
_storyboard.Completed += callback;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除动画完成回调
|
|
/// </summary>
|
|
public void RemoveCompletedCallback(EventHandler callback)
|
|
{
|
|
_storyboard.Completed -= callback;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查动画是否正在运行
|
|
/// </summary>
|
|
public bool IsAnimating => _storyboard.GetCurrentState() == ClockState.Active;
|
|
|
|
/// <summary>
|
|
/// 设置缓动函数
|
|
/// </summary>
|
|
public void SetEasingFunction(IEasingFunction easingFunction)
|
|
{
|
|
_thicknessAnimation.EasingFunction = easingFunction;
|
|
}
|
|
}
|
|
} |