C# 设置文本动画,使字母在圆圈中循环

C# 设置文本动画,使字母在圆圈中循环,c#,wpf,xaml,C#,Wpf,Xaml,我有一个包含字母ABC的文本框 我想做的是让信件传阅。顺时针C是第一个开始循环的字母,因为C是绕B的一部分,所以B开始循环,然后A。当C到达顶部时,我希望它迅速停止,直到B,然后A回来,这样我们又有了ABC。然后重复——希望这是有意义的 下面是我的代码(大部分是复制的)。目前所发生的一切是C做一个圆圈,然后停止,然后再去,但a&B似乎没有移动 C# void StartTextAnimations(对象发送器、路由目标) { txtABC.TextEffects=新的TextEffectColl

我有一个包含字母ABC的文本框

我想做的是让信件传阅。顺时针C是第一个开始循环的字母,因为C是绕B的一部分,所以B开始循环,然后A。当C到达顶部时,我希望它迅速停止,直到B,然后A回来,这样我们又有了ABC。然后重复——希望这是有意义的

下面是我的代码(大部分是复制的)。目前所发生的一切是C做一个圆圈,然后停止,然后再去,但a&B似乎没有移动

C#

void StartTextAnimations(对象发送器、路由目标)
{
txtABC.TextEffects=新的TextEffectCollection();
情节提要sbRotate=新情节提要();
sbRotate.RepeatBehavior=RepeatBehavior.Forever;
sbRotate.AutoReverse=false;
for(int i=0;i
XAML


问题在于,每次使用
FindResource
时,您都会使用相同的双动画,并实际更改其属性。您要做的是对每个角色应用效果,这意味着每个角色都需要自己的
DoubleAnimation
实例。这是我使用的工作示例(更改了前景,以便我可以实际看到危险的东西;))

  • StartTextAnimations
    中:更改for循环,以便开始以正确的顺序应用动画,从“c”开始:

  • 更改
    AddRotationAnimation
    以将动画应用于每个角色:

    void AddRotationAnimation(Storyboard sbRotate, int charIndex)
    {
        DoubleAnimation anim = new DoubleAnimation(0, 360, new Duration(new TimeSpan(0, 0, 0, 3))); //0 to 360 over 3 seconds
        Storyboard.SetTargetName(anim, "txtABC"); 
        SetBeginTime(anim, charIndex);
    
        string path = string.Format("TextEffects[{0}].Transform.Children[1].Angle", charIndex);
    
        PropertyPath propPath = new PropertyPath(path);
    
        Storyboard.SetTargetProperty(anim, propPath);
    
        sbRotate.Children.Add(anim);
    }
    
  • SetBeginTime
    中更改您的计算,使字符不会相互重叠

    void SetBeginTime(Timeline anim, int charIndex)
    {
        double totalMs = anim.Duration.TimeSpan.TotalMilliseconds;
        double offset = totalMs / 4.2; //slow, it, down.
        double resolvedOffset = offset * charIndex;
        anim.BeginTime = TimeSpan.FromMilliseconds(resolvedOffset);
    }
    
证明:


你玩过路径动画吗?没有-老实说,我还没有听说过。从未使用过动画,所以请原谅我的无知。我现在就用谷歌搜索,不用担心,这就是为什么会有这么多人。听起来这会让你的生活更轻松。非常感谢你的回答,非常清楚。我只是想知道,在每次循环后暂停动画一秒钟,然后再重新开始是否容易?
//Reverse order applied
for(int i = txtABC.Text.Length - 1; i >= 0; i--)
{
    AddTextEffectForCharacter(i);
    AddRotationAnimation(sbRotate, i);
}
void AddRotationAnimation(Storyboard sbRotate, int charIndex)
{
    DoubleAnimation anim = new DoubleAnimation(0, 360, new Duration(new TimeSpan(0, 0, 0, 3))); //0 to 360 over 3 seconds
    Storyboard.SetTargetName(anim, "txtABC"); 
    SetBeginTime(anim, charIndex);

    string path = string.Format("TextEffects[{0}].Transform.Children[1].Angle", charIndex);

    PropertyPath propPath = new PropertyPath(path);

    Storyboard.SetTargetProperty(anim, propPath);

    sbRotate.Children.Add(anim);
}
void SetBeginTime(Timeline anim, int charIndex)
{
    double totalMs = anim.Duration.TimeSpan.TotalMilliseconds;
    double offset = totalMs / 4.2; //slow, it, down.
    double resolvedOffset = offset * charIndex;
    anim.BeginTime = TimeSpan.FromMilliseconds(resolvedOffset);
}