繁体   English   中英

在C#中以编程方式轻松创建Silverlight动画

[英]Programmatically create silverlight animation with easing in C#

我正在用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();
    }

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

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

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

跳过问题可能是启动要在不到一秒钟的时间内显示更改的情节提要的延迟。 等到情节提要板准备好显示时,它基本上会说:“哦……现在过了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秒长的情节提要,并缓缓倾斜(将继续研究该问题)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM