QPropertyAnimation可以简单方便的实现对象的旋转和移动的动画效果。
1. 移动
Pixmap *item = new Pixmap(kineticPix);
QPropertyAnimation *animation = new QPropertyAnimation(item, "pos");
anim1->setStartValue(QPoint(-100, -100));
anim1->setEndValue(QPoint(500, 100));
anim1->setEasingCurve(QEasingCurve::Linear);
connect(anim1, SIGNAL(finished()), this, SLOT(EndAnimation())); //动画结束后需要执行的函数
anim1->start(QAbstractAnimation::KeepWhenStopped);
2. 旋转
想要实现旋转的动画效果,要旋转的对象就必须支持‘’rotation‘’属性,否则无法实现旋转动画。需要在类中定义:
Q_PROPERTY(QPointF pos READ pos WRITE setPos) //移动
Q_PROPERTY(int rotation READ rotation WRITE setRotation) //旋转
然后同理处理旋转:
QPropertyAnimation *animation = new QPropertyAnimation(item, "rotation");
animation->setDuration(2000);
animation->setStartValue(0);
animation->setEndValue(90);
animation->setLoopCount(1); //旋转次数
connect(animation, SIGNAL(finished()), this, SLOT(onAnimation()));
animation->start(QAbstractAnimation::KeepWhenStopped);