目录
1、效果
2、核心源码
#include <QPropertyAnimation>
QPropertyAnimation *m_leftAnimation;
QPropertyAnimation *m_rightAnimation;
m_leftAnimation = new QPropertyAnimation(ui->lab_left,"pos");
m_rightAnimation = new QPropertyAnimation(ui->lab_right,"pos");
int duration = 2000;
m_leftAnimation->setDuration(duration);
m_rightAnimation->setDuration(duration);
m_leftAnimation->setStartValue(QPoint(0,0));
m_leftAnimation->setEndValue(QPoint(-width/2,0));
m_rightAnimation->setStartValue(QPoint(width/2,0));
m_rightAnimation->setEndValue(QPoint(width,0));
m_leftAnimation->setEasingCurve(QEasingCurve::InOutCirc);
m_rightAnimation->setEasingCurve(QEasingCurve::InOutCirc);
m_leftAnimation->start();
m_rightAnimation->start();
3、QPropertyAnimation探究
QPropertyAnimation 对 Qt 属性进行插值。由于属性值存储在 QVariant 中,该类继承了 QVariantAnimation,并支持与其超类相同元类型的动画。
声明属性的类必须是 QObject。为了能够为属性设置动画,它必须提供一个 setter(以便 QPropertyAnimation 可以设置属性的值)。这使得为许多 Qt 小部件制作动画成为可能
3.1、官方示例
QPropertyAnimation *animation = new QPropertyAnimation(myWidget, "geometry");
animation->setDuration(10000);
animation->setStartValue(QRect(0, 0, 100, 30));
animation->setEndValue(QRect(250, 250, 100, 30));
animation->start();
3.2、属性成员
1、propertyName : QByteArray
此属性定义动画的目标属性名称。动画运行需要属性名称。
2、targetObject : QObject*
此属性定义动画的目标 QObject。
3.3、成员函数
1、QPropertyAnimation(QObject *target, const QByteArray &propertyName, QObject *parent = nullptr)
构造一个 QPropertyAnimation 对象。parent 传递给 QObject 的构造函数。动画更改目标上的属性 propertyName。默认持续时间为 250 毫秒。
2、void updateCurrentValue(const QVariant &value)
每当当前值发生变化时,都会调用这个虚函数。value 是更新的值。它更新目标对象上属性的当前值。
3、void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
如果在动画状态从 Stopped 变为 Running 时未定义 startValue,则将当前属性值用作动画的初始值。