使用计时器和毫秒来淡化表格,但持续时间加倍

问题描述 投票:1回答:1

我正在制作我自己的小自定义消息框,基本上它只是一个显示X时间消息的小方框,然后逐渐淡出Y持续时间。发生的事情是,消失的时间是其应有的两倍,我无法弄清楚原因。有人可以看看我的代码并发现为什么它花费的时间比预期的消失时间长一倍吗?

    //Initiate and set up the message bubble.
    public static void InitiateBubble(String displayText, Double showTime = 1000, Double fadeTime = 2000) {
        Bubble bubble = new Bubble(displayText);
        bubble.showTime = showTime;
        bubble.fadeTime = fadeTime;
        bubble.Show();
        bubble.showTimer = new Timer();
        bubble.showTimer.Interval = (int)bubble.showTime;
        bubble.showTimer.Tick += bubble.startFadeAway;
        bubble.showTimer.Start();
    }

    //Leaves some time on screen before starting to fade away
    private void startFadeAway(object sender, EventArgs e) {
        showTimer.Stop();
        fadeAwayTimer = new Timer();
        fadeAwayTimer.Interval = 10;
        fadeAwayTimer.Tick += fadeAway;
        fadeAwayTimer.Start();
    }

    //slowly fades the contorle away until it disapears.
    private void fadeAway(object sender, EventArgs e) {
        double opacity = Opacity;
        opacity -= (10 / fadeTime);
        if (opacity < 0) {
            Close();
        }
        else {
            Opacity = opacity;
        }
    }
c# timer fade
1个回答
2
投票

如果用户将淡入淡出间隔设置为1秒(1000毫秒),并且我们将定时器间隔设置为1/10秒(100毫秒),那么我们需要将每个间隔的不透明度淡化10%(因为间隔是每秒触发10次)。所以我们会在每次迭代时设置Opacity -= .1

如果用户将淡入淡出间隔设置为2秒(2000毫秒),并且我们仍将定时器间隔设置为1/10秒,那么我们需要将每个间隔的不透明度淡化5%,因此我们将设置Opacity -= .05在每次迭代。

看到这种关系,我们可以发现:

var amountToReduceOpacity = 1.0 / fadeTime * interval;

注意:正如上面提到的γηράσκω δ' αεί πολλά διδασκόμε,winform计时器的分辨率大约是17毫秒,所以如果我们将其设置为小于此值,则淡入淡出将大大减慢,因为我们将计算一个非常快的计时器的速率(意味着我们每次迭代都不会褪色),但执行速度会慢一些。在我的机器上,将其设置为50看起来很好。

现在我们可以使用这个公式来始终以每个间隔的正确数量淡化表格。这里有一个Form样本,基本上就是你在上面做的事情(注意我在表格上放了一个标签和两个计时器,并命名为:lblDisplayshowTimerfadeTimer):

public partial class Bubble : Form
{
    private readonly double amountToReduceOpacity;
    private readonly int fadeInterval = 50; 

    // Custom form constructor takes in all three required settings
    public Bubble(string displayText, int showTime, int fadeTime)
    {
        InitializeComponent();

        lblDisplay.AutoSize = true;
        lblDisplay.Text = displayText;
        lblDisplay.Left = ClientRectangle.Width / 2 - lblDisplay.Width / 2;
        lblDisplay.Top = ClientRectangle.Height / 2 - lblDisplay.Height / 2;

        showTimer.Interval = showTime;
        fadeTimer.Interval = fadeInterval;

        amountToReduceOpacity = 1.0 / fadeTime * fadeInterval;
    }

    // The Shown event starts the first timer
    private void Bubble_Shown(object sender, EventArgs e)
    {
        showTimer.Start();
    }

    // The shownTimer starts the fadeTimer
    private void showTimer_Tick(object sender, EventArgs e)
    {
        showTimer.Stop();
        BackColor = Color.Red; // Just so we see when the fade starts
        fadeTimer.Start();
    }

    // The fade timer reduces opacity on each iteration until it's zero
    private void fadeTimer_Tick(object sender, EventArgs e)
    {
        Opacity -= amountToReduceOpacity;
        if (Opacity <= 0) Close();            
    }
}

然后在客户端我们可以做类似的事情:

private void button1_Click(object sender, EventArgs e)
{
    Bubble bubble = new Bubble("Help me, I'm Fading!", 1000, 2000);
    bubble.Show();
}
© www.soinside.com 2019 - 2024. All rights reserved.