由于某种原因,平滑按钮移动代码不起作用。代码本身
private static void AnimationTransform (Button cont, Point firstPoint, Point endPoint, int frame, float seconds)
{
cont.Location = firstPoint;
seconds = seconds * 1000;
float delay = seconds / frame;
Point distance = new Point(endPoint.X - firstPoint.X, endPoint.Y - firstPoint.Y);
(float, float) frameDistance = (distance.X / frame, distance.Y / frame);
(float, float) currPoint = (firstPoint.X, firstPoint.Y);
for (int i = 0; i < frame; i ++)
{
Thread.Sleep((int)delay);
currPoint = (currPoint.Item1 + frameDistance.Item1, currPoint.Item2 + frameDistance.Item2);
cont.Location = new Point((int)currPoint.Item1, (int)currPoint.Item1);
}
cont.Location = endPoint;
}
private void button10_Click(object sender, EventArgs e)
{
AnimationTransform(button11, button10.Location, button11.Location, 50, 6);
}
问题是什么?
为了在代码执行时更新表单,我们需要使动画中的等待异步。