下面代码的目的是拇指跟随鼠标水平移动。该代码在鼠标事件上被调用,因此动画的目标值不断更新。
在代码中,offset
是当前鼠标的水平位置。问题是, thumb 的动画没有完全动画到指定的offset
,但似乎总是停在一个更小或更高的值上(取决于鼠标是向左还是向右拖动)。
影响动画的SeekAlignedToLastTick()
行为,虽然我无法通过阅读文档来弄清楚这个函数的作用。
如何为拇指设置动画,使其平滑地跟随拖动事件?
private Storyboard _thumbStoryboard;
private DoubleAnimation _thumbAnimation = new DoubleAnimation();;
private CompositeTransform _thumbTransform = new CompositeTransform();
private void UpdateUserInterface(double offset)
{
var thumbItem = Thumb as FrameworkElement;
if (_thumbStoryboard == null)
{
Storyboard.SetTarget(_thumbAnimation, _thumbTransform);
_thumbStoryboard = new Storyboard();
_thumbStoryboard.Children.Add(_thumbAnimation);
thumbItem.RenderTransform = _thumbTransform;
_thumbStoryboard.Duration = new Duration(TimeSpan.FromMilliseconds(100));
_thumbAnimation.EasingFunction = new ExponentialEase();
}
double from = _thumbTransform.TranslateX;
_thumbStoryboard.Stop();
Storyboard.SetTargetProperty(_thumbAnimation, new PropertyPath("TranslateX"));
_thumbAnimation.From = from;
_thumbAnimation.To = offset;
_thumbStoryboard.Begin();
_thumbStoryboard.SeekAlignedToLastTick(TimeSpan.Zero);
}