How to animate at periodic intervals?

蹲街弑〆低调 提交于 2019-12-24 00:09:40

问题


I have a storyboard which animates a minute hand to slide 6 degrees. Now i want the minute hand to slide at every 59th second forever. Are there any properties of storyboard or any other way that i can do it?

My storyboard

<Storyboard
                                    x:Name="myStoryboard2">
                                    <DoubleAnimation
                                        x:Name="minuteAnimation"
                                        Storyboard.TargetName="minHandTransform"
                                        Storyboard.TargetProperty="Angle"
                                        Duration="0:0:1"
                                        From="{Binding Time, Converter={StaticResource minuteHandTransform}}"
                                        To="{Binding Time, Converter={StaticResource minuteHandTransform}}"
                                        RepeatBehavior="1x">
                                        <DoubleAnimation.EasingFunction>
                                            <SineEase
                                                EasingMode="EaseOut" />
                                        </DoubleAnimation.EasingFunction>
                                    </DoubleAnimation>
                                </Storyboard>

回答1:


It doesn't sound like something you would want to rely on an animation to manage. Simply manage starting the animation from code behind every minute and you are done. It would be a lot easier to do it that way than using cryptic converters to control the From/To values. A Timeline such as a DoubleAnimation has a BeginTime property, but I have seen and verified reports of long animation durations (like 1 minute or above) hitting bugs in WinRT.

EDIT* (code samples)

Two simple ways I commonly use to trigger events at an interval are to use a DispatcherTimer with callback events or an async loop.

1.  DispatcherTimer

var timer = new DispatcherTimer { Interval = TimeSpane.FromSeconds(1) };
timer.Tick += (s, e) => { /* do your stuff */ };
timer.Start();

2.  async loop

RunMyLoop();

private async void RunMyLoop()
{
    while (true)
    {
        /* do your stuff */
        await Task.Delay(1000);
    }
}



回答2:


Try the following:

<Storyboard
                                x:Name="myStoryboard2">
                                <DoubleAnimation
                                    x:Name="minuteAnimation"
                                    Storyboard.TargetName="minHandTransform"
                                    Storyboard.TargetProperty="Angle"
                                    Duration="0:0:59"
                                    From="{Binding Time, Converter={StaticResource minuteHandTransform}}"
                                    To="{Binding Time, Converter={StaticResource minuteHandTransform}}"
                                    RepeatBehavior="Forever">
                                    <DoubleAnimation.EasingFunction>
                                        <SineEase
                                            EasingMode="EaseOut" />
                                    </DoubleAnimation.EasingFunction>
                                </DoubleAnimation>
                            </Storyboard>


来源:https://stackoverflow.com/questions/14273506/how-to-animate-at-periodic-intervals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!