gpt4 book ai didi

c# - 缓动动画 'twitches' 完成时

转载 作者:太空狗 更新时间:2023-10-29 23:10:12 24 4
gpt4 key购买 nike

我正在使用 Microsoft Interactivity 和 Microsoft Interactions 根据代码隐藏中的属性旋转对象。为了使旋转更平滑,我添加了一个缓动函数。它的动画效果非常好,但是当它到达 1 个分割帧的动画结束时,旋转将重置为动画之前的值,然后切换回旋转后的值,导致它来回“抽动” .这只发生在 EaseOut 上。

<i:Interaction.Triggers>
<ie:PropertyChangedTrigger Binding="{Binding Rotation}">
<ie:ChangePropertyAction TargetName="RotateTransformer" PropertyName="Angle" Value="{Binding Rotation}" Duration="0:0:2">
<ie:ChangePropertyAction.Ease>
<BackEase EasingMode="EaseOut" Amplitude="1.2" />
</ie:ChangePropertyAction.Ease>
</ie:ChangePropertyAction>
</ie:PropertyChangedTrigger>
</i:Interaction.Triggers>
<Path Stroke="Black" Fill="Gray">
<Path.RenderTransform>
<RotateTransform x:Name="RotateTransformer" CenterX="64" CenterY="105" />
</Path.RenderTransform>
<Path.Data>
<PathGeometry>
<PathFigureCollection>
<PathFigure StartPoint="64,0" >
<LineSegment Point="39,110" />
<LineSegment Point="64, 70" />
<LineSegment Point="39,180" />
<LineSegment Point="89, 180" />
<LineSegment Point="64,70"/>
<LineSegment Point="89,110" />
<LineSegment Point="64,0" />
</PathFigure>
</PathFigureCollection>
</PathGeometry>
</Path.Data>
</Path>

最佳答案

由于这似乎是 ChangePropertyAction 类实现中的一个错误,我想弄清楚这个问题的最佳方法是将程序集放入您最喜欢的反射器样式应用程序中,然后查看实现的内部结构.

这是一段摘录(遗漏了很多,但相关的部分还是在里面):

  public class ChangePropertyAction : TargetedTriggerAction<object>
{
/* some dependency properties here, like DurationProperty, ValueProperty, etc... */


protected override void Invoke(object parameter)
{
/* a lot of validation here, but skimming over that mostly. Valid input results in a call to AnimatePropertyChange() */
}

private void AnimatePropertyChange(PropertyInfo propertyInfo, object fromValue, object newValue)
{
Storyboard storyboard = new Storyboard();
Timeline timeline = !typeof (double).IsAssignableFrom(propertyInfo.PropertyType)
? (!typeof (Color).IsAssignableFrom(propertyInfo.PropertyType)
? (!typeof (Point).IsAssignableFrom(propertyInfo.PropertyType)
? this.CreateKeyFrameAnimation(fromValue, newValue)
: this.CreatePointAnimation((Point) fromValue, (Point) newValue))
: this.CreateColorAnimation((Color) fromValue, (Color) newValue))
: this.CreateDoubleAnimation((double) fromValue, (double) newValue);
timeline.Duration = this.Duration;
storyboard.Children.Add(timeline);
Storyboard.SetTarget((Timeline) storyboard, (DependencyObject) this.Target);
Storyboard.SetTargetProperty((Timeline) storyboard, new PropertyPath(propertyInfo.Name, new object[0]));
storyboard.Completed += (EventHandler) ((o, e) => propertyInfo.SetValue(this.Target, newValue, new object[0]));
storyboard.FillBehavior = FillBehavior.Stop;
storyboard.Begin();
}

private static object GetCurrentPropertyValue(object target, PropertyInfo propertyInfo)
{
FrameworkElement frameworkElement = target as FrameworkElement;
target.GetType();
object obj = propertyInfo.GetValue(target, (object[]) null);
if (frameworkElement != null && (propertyInfo.Name == "Width" || propertyInfo.Name == "Height") && double.IsNaN((double) obj))
obj = !(propertyInfo.Name == "Width") ? (object) frameworkElement.ActualHeight : (object) frameworkElement.ActualWidth;
return obj;
}

private Timeline CreateDoubleAnimation(double fromValue, double newValue)
{
return (Timeline) new DoubleAnimation()
{
From = new double?(fromValue),
To = new double?(newValue),
EasingFunction = this.Ease
};
}
}

如果您想查看完整代码,请自己通过 DotPeek 或 ILSpy 运行它,两者都是免费的:-)

所以最后,它所做的只是验证输入、查看值的类型并创建一个 Storyboard,其中包含适合属性类型的过渡动画。“闪烁”效果实际上是在动画完成时值短暂返回到其原始值(实际绑定(bind)的值),之后更新绑定(bind)以反射(reflect)新值。这种行为的原因归结为 Storyboard上的一个属性设置:

storyboard.FillBehavior = FillBehavior.Stop;

此 FillBehavior 确定时间轴(在本例中为 Storyboard)结束时发生的情况。 MSDN 是这样说的:

HoldEnd: After it reaches the end of its active period, the timeline holds its progress until the end of its parent's active and hold periods.

Stop: The timeline stops if it is outside its active period while its parent is inside its active period.

如果我们简单地将此属性更改为 FillBehavior.HoldEnd,闪烁就会消失。缺点是您必须重新实现此 TriggerAction,但如果您只想让它为双动画工作,您可能会省略很多内容。

希望这对任何人都有帮助!

关于c# - 缓动动画 'twitches' 完成时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7834056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com