QPropertyAnimation实现图形,控件的旋转和位移动画,尤其是旋转

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

猜你喜欢

转载自www.cnblogs.com/godfaber/p/9036648.html