计算系数以获得正常速度的动画 [英] Calculate coefficient to get normal speed animation

查看:43
本文介绍了计算系数以获得正常速度的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一些WPF动画在垂直方向上移动UserControl.

I use some WPF animation to move UserControl within vertically.

这是代码

public void RestartAnimation(double contentControlHeight, double thisHeight)
        {
            if (cp != null && IsLoaded)
                da.From = contentControlHeight;  
                da.To = -thisHeight;
                da.RepeatBehavior = RepeatBehavior.Forever;
                da.Duration = new Duration(TimeSpan.FromSeconds(this.Duration)); 
                sb.Children.Clear();
                sb.Children.Add(da);
                Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Top)"));
                Storyboard.SetTarget(da, cp);
                sb.Begin();           
            }
        }

效果很好,但是我发现高度越大,运动速度就越快.

所以我需要意识到两件事:

So I need to realize 2 things:

一些速度范围值,即1-100(非常慢-超快),内部需要一些公式/系数.

Some speed range values ie 1-100 (very slow - super fast) and internally I need some formula/coefficient to do it.

我用静态速度和不同高度做了一些实验,并得到了一张桌子.

I did some experiments with the static speed and different height and got some table.

请帮助我确定我只需要设置速度范围(1-100)即可进行一次计算,并且无论使用什么都可以正常工作 StackPanel 高度.

谢谢你们!

推荐答案

我不确定您是否应该对比例(StackPanel Height/ContentControl Height)感兴趣,而只需对 difference (StackPanel高度-ContentControl高度).

I'm not sure you should be interested in the ratio (StackPanel Height / ContentControl Height), but rather simply the difference (StackPanel Height - ContentControl Height).

假设您希望速度为每秒30像素(实际上相当慢,但仅来自上面的速度"值).您可能会想以每秒30像素的速度覆盖像素距离(高度差)所需的秒数来设置持续时间.

Say that you want your speed to be 30 pixels per second (actually rather slow, but just going from your "speed" value above). You would be interested in setting your duration based on how many seconds it will take to cover the pixel distance (the difference in heights) at a rate of 30 pixels per second.

所以您只需要:

var duration = TimeSpan.FromSeconds((thisHeight - contentControlHeight) / pixelPerSecondSpeed);

这就是使速度保持恒定的方式.

This is how you would leave speed at a constant.

这篇关于计算系数以获得正常速度的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆