c# Winform 缓动动画
2024-09-08 18:59:00
一、定义缓动动画类
public class AnimationHelper
{
Timer animationTimer = new Timer();
double velocity = 0.0;
Point location = Point.Empty;
double force = 0.01; //0.69;
double drag = 0.8;
private Control control;
//private AxShockwaveFlash control;
private int targetPos=0; public AnimationHelper(int interval=5)
{
animationTimer.Interval = interval;
animationTimer.Tick += delegate
{
if (control == null) return;
int currentPos = control.Location.X;
if (Math.Abs(currentPos - targetPos) <= 5)
{
control.Location = new Point(targetPos, control.Location.Y);
animationTimer.Stop();
}
int dx = targetPos - currentPos;
velocity += force * dx;
velocity *= drag;
if (Math.Abs(velocity) < 5)
{
if (velocity > 0)
velocity = 5;
else
velocity = -5;
}
control.Location = new Point(currentPos + (int)velocity, control.Location.Y);
};
animationTimer.Start();
} public void MoveXEx(Control control, int targetPos, Action completeWith = null)
{
this.control = control;
this.targetPos = targetPos;
velocity = 0;
animationTimer.Start();
}
}
二、’使用教程:
AnimationHelper animationHelper = new AnimationHelper();
AnimationHelper animationHelper1 = new AnimationHelper();
private int pos = 0;
private int pos1 = 0;
private int dx = 200;
private void button1_Click(object sender, EventArgs e)
{
animationHelper.MoveXEx(pictureBox2, pos += dx);
animationHelper1.MoveXEx(pictureBox1, pos1 += dx); } private void button2_Click(object sender, EventArgs e)
{
animationHelper.MoveXEx(pictureBox2, pos -= dx);
animationHelper1.MoveXEx(pictureBox1, pos1 -= dx);
}
三、效果:
最新文章
- pod Spec管理配置
- export a java project to runable jar
- python数据结构-基本数据类型
- Rose
- 关键字throw(something)限制
- python - socket练习(输入系统命令)
- 性能优化工具---iostat
- boost.xml_parser中文字符问题
- BZOJ 1975 SDOI2010 魔法猪学院 A*k短路
- UVa 572 Oil Deposits(DFS)
- 使用KindEditor富文本编辑器,点击批量上传按钮没有选择图片按钮
- js关于setTimeout传参
- 文档发布工具mkdocs
- 什么是Servlet(原理,从访问到方法)
- Python descriptor 以及 内置property()函数
- Nokia Imaging SDK
- $思维导图——numpy基本知识
- 安装了IntelliJ IDEA中文输入法光标不跟随处理办法
- HDU 4609 FFT模板
- HDU 1238 Substing
热门文章
- 问题记录:VMware vSphere vCenter 7.0 上传文件失败
- 11月15日内容总结——软件开发架构、网络编程介绍和OSI七层协议介绍
- 10月25日内容总结——正则表达式相关知识与re模块
- 翻译《threejsfundamentals》离屏渲染+web-worker一篇
- Eclpis-cannot open git-receive-pack
- xml基本学习
- STM32F0库函数初始化系列:PWM输出
- 软赢软件控制器在Zstack云主机实现电机控制的方法
- 使用Docker搭建EPICS的IOC记录
- 学习ASP.NET Core Blazor编程系列二十七——JWT登录(1)