【问题标题】:Programmatically create silverlight animation with easing in C#在 C# 中以编程方式创建带有缓动的 Silverlight 动画
【发布时间】:2010-08-22 03:32:17
【问题描述】:

我正在用 Silverlight 制作时钟。我正在尝试以编程方式设置动画代码,因为我想为 3 个时钟指针中的每一个重复使用相同的代码(而且我认为我不能使用 xaml 中的单个故事板来做到这一点)。

        public void Rotate(double toAngle, RotateTransform rotate)
    {
        Storyboard sb = new Storyboard();

        DoubleAnimationUsingKeyFrames keyframes = new DoubleAnimationUsingKeyFrames();

        EasingDoubleKeyFrame easingStart = new EasingDoubleKeyFrame();
        easingStart.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
        easingStart.Value = rotate.Angle;

        EasingDoubleKeyFrame easingEnd = new EasingDoubleKeyFrame();
        easingEnd.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5));
        easingEnd.Value = toAngle;
        var ease = new ElasticEase();
        ease.EasingMode = EasingMode.EaseIn;
        easingEnd.EasingFunction = ease;

        keyframes.KeyFrames.Add(easingStart);
        keyframes.KeyFrames.Add(easingEnd);

        Storyboard.SetTarget(keyframes, rotate);
        Storyboard.SetTargetProperty(keyframes, new PropertyPath("(RotateTransform.Angle)"));

        sb.Children.Add(keyframes);
        sb.Begin();
    }

我传入了我希望当前手旋转到的角度和该手的旋转变换。从这个旋转变换中,我得到了起始角度。

当我运行时钟时,时钟指针会移动(秒针每秒移动到正确的位置等),但动画似乎并没有实际动画。他们只是从头到尾立即结束。

为什么动画不能正常发生?

【问题讨论】:

    标签: c# silverlight-4.0


    【解决方案1】:

    跳过问题很可能是在启动想要在不到一秒的时间内显示更改的情节提要时出现延迟。当故事板准备好显示时,它基本上会说“哦......现在已经过了 1 秒,我现在应该在哪里”。故事板越长越好。

    我不确定你想要多少控制你的时钟,但关于使用单个故事板,这里是......

    我不确定您要解决的整体问题,但以下示例使用一个简单的情节提要来运行时钟的 3 个指针。我遇到的唯一问题是我可以为时针(在 XAML 中)指定的最大时间间隔是 23:59:59,而不会出现解析错误。

        <Storyboard x:Name="SecondHandStoryboard" RepeatBehavior="Forever">
            <DoubleAnimation Duration="0:1:0" To="360" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="SecondHand" />
            <DoubleAnimation Duration="1:0:0" To="360" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="MinuteHand"/>
            <DoubleAnimation Duration="23:59:59" To="360" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="HourHand"/>
        </Storyboard>
    

    我实际上想为秒针添加一个坚实的“滴答”动画,但还没有弄清楚如何让“按”故事板旋转工作。基本上使用重复的每秒 6 度 1 秒的长故事板,并带有陡峭的缓动(将继续研究这个问题)。

    无论如何,希望这对您有所帮助。

    【讨论】:

    • 所以问题是,你想要劳力士还是仿制品?有时,仿制品太难创造了。 :)
    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多