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);