zoukankan      html  css  js  c++  java
  • Qt之QSequentialAnimationGroup

    简述

    QSequentialAnimationGroup类提供动画的串行组。

    QSequentialAnimationGroup是一个串行运行动画的QAnimationGroup,在另一个动画播放结束之后,开启一个新的动画,根据添加到动画组的顺序(使用addAnimation() 或 insertAnimation())来播放动画,当最后一个动画完成以后,动画组随之完成。

    在动画组中每一个时刻最多只有一个动画处于激活状态,可以通过currentAnimation()获得,空组则没有当前动画。

    详细描述

    一个串行动画组可以被当做任何其它动画,即:它可以被开始、停止、添加到其它动画组中,也可以通过addPause() 或 insertPause()为动画组添加一个暂停。

    QSequentialAnimationGroup *group = new QSequentialAnimationGroup;
    
    group->addAnimation(anim1);
    group->addAnimation(anim2);
    
    group->start();

    这个例子中,anim1、anim2是QPropertyAnimation。

    公共函数

    • QPauseAnimation * addPause(int msecs)
      为动画组添加一个msecs毫秒级的暂停。暂停被认为是一种特殊类型的动画,因此animationCount将增加一个。

    • QAbstractAnimation * currentAnimation() const
      返回大年时刻的动画

    • QPauseAnimation * insertPause(int index, int msecs)
      为动画组添加一个msecs毫秒级的暂停,index为动画索引。

    信号

    • void currentAnimationChanged(QAbstractAnimation * current)
      当前动画发生变化时,发射此信号。

    示例

    下面,我们通过QSequentialAnimationGroup来构建一个串行动画组,并添加属性动画QPropertyAnimation,这里也可以使用addAnimation()添加其它动画/动画组,就不予演示了。

    效果

    这里写图片描述

    源码

    MainWindow::MainWindow(QWidget *parent)
        : CustomWindow(parent)
    {
        ...
    
        QPushButton *pStartButton = new QPushButton(this);
        pStartButton->setText(QString::fromLocal8Bit("开始动画"));
    
        QList<QLabel *> list;
        QStringList strList;
        strList << QString::fromLocal8Bit("一去丶二三里") << QString::fromLocal8Bit("青春不老,奋斗不止");
    
        for (int i = 0; i < strList.count(); ++i)
        {
            QLabel *pLabel = new QLabel(this);
            pLabel->setText(strList.at(i));
            pLabel->setAlignment(Qt::AlignCenter);
            list.append(pLabel);
        }
    
        // 动画一
        QPropertyAnimation *pAnimation1 = new QPropertyAnimation(list.at(0), "geometry");
        pAnimation1->setDuration(1000);
        pAnimation1->setStartValue(QRect(0, 0, 100, 30));
        pAnimation1->setEndValue(QRect(120, 130, 100, 30));
        pAnimation1->setEasingCurve(QEasingCurve::OutBounce);
    
        // 动画二
        QPropertyAnimation *pAnimation2 = new QPropertyAnimation(list.at(1), "geometry");
        pAnimation2->setDuration(1000);
        pAnimation2->setStartValue(QRect(120, 130, 120, 30));
        pAnimation2->setEndValue(QRect(170, 0, 120, 30));
        pAnimation2->setEasingCurve(QEasingCurve::OutInCirc);
    
        m_pGroup = new QSequentialAnimationGroup(this);
    
        // 添加动画
        m_pGroup->addAnimation(pAnimation1);
    
        // 暂停1秒
        m_pGroup->addPause(1000);
        m_pGroup->addAnimation(pAnimation2);
    
        // 循环2次
        m_pGroup->setLoopCount(2);
    
        // 从后向前执行
        m_pGroup->setDirection(QAbstractAnimation::Backward);
    
        // 连接信号槽
        connect(m_pGroup, SIGNAL(currentAnimationChanged(QAbstractAnimation*)),
                this, SLOT(onCurrentAnimationChanged(QAbstractAnimation*)));
    
        connect(pStartButton, SIGNAL(clicked(bool)), this, SLOT(startAnimation()));
    
        ...
    }

    槽函数如下:

    // 开始动画
    void MainWindow::startAnimation()
    {
        m_pGroup->start();
    }
    
    // 动画切换时会调用
    void MainWindow::onCurrentAnimationChanged(QAbstractAnimation *current)
    {
        QPropertyAnimation *pAnimation = dynamic_cast<QPropertyAnimation *>(current);
        if (pAnimation == NULL)
            return;
    
        QLabel *pLabel = dynamic_cast<QLabel *>(pAnimation->targetObject());
        if (pLabel != NULL)
            pLabel->setText(pLabel->text() + ".");
    }

    这里需要注意,如上所属addPause()之后,暂停也是一个动画(比较特殊而已),所以我们需要用dynamic_cast来转换,并判断是否为NULL(否则会crash)。

    更多参考

  • 相关阅读:
    2015-3-27
    2015-3-26
    2015-3-25
    2015-3-24
    oracle数据字典表 系统表
    PowerDesigner教程系列(六)概念数据模型
    PowerDesigner教程系列(五)概念数据模型
    PowerDesigner教程系列(四)概念数据模型
    PowerDesigner教程系列(三)概念数据模型
    PowerDesigner教程系列(二)概念数据模型
  • 原文地址:https://www.cnblogs.com/new0801/p/6146582.html
Copyright © 2011-2022 走看看