【问题标题】:QPropertyAnimation: Immedately jump to end of animation?QPropertyAnimation:立即跳转到动画结尾?
【发布时间】:2012-07-14 20:41:03
【问题描述】:

我使用 QPropertyAnimation 为用户输入动画以在小部件中导航。也就是说,我用它来平滑鼠标滚轮的缩放。

目前,当用户提供新输入(旋转鼠标滚轮)时,当前动画将被取消并开始一个新动画,从我的缩放属性的当前值开始。

例如,如果放大操作将视图缩放 2 倍,我们可以想象以下场景:

   User input         |  Zoom before the animation  |  Animation's end value
----------------------+-----------------------------+--------------------------
   Mouse wheel up     |                      100 %  |                  200 %
   (wait)             |                             |
   Mouse wheel up     |                      200 %  |                  400 %
   (wait)             |                             |
   Mouse wheel up     |                      400 %  |                  800 %

但如果用户不等待动画完成:

   User input         |  Zoom before the animation  |  Animation's end value
----------------------+-----------------------------+--------------------------
   Mouse wheel up     |                      100 %  |                  200 %
   Mouse wheel up     |                      110 %  |                  220 %
   Mouse wheel up     |                      120 %  |                  240 %

我想要的(同样,用户不会等待):

   User input         |  Zoom before the animation  |  Animation's end value
----------------------+-----------------------------+--------------------------
   Mouse wheel up     |                      100 %  |                  200 %
   Mouse wheel up     |  immediately jump to 200 %  |                  400 %
   Mouse wheel up     |  immediately jump to 400 %  |                  800 %

我讨厌在动画结束之前无法进行进一步用户输入的应用程序,因此我最讨厌流畅的动画。所以我想要的是:当用户给另一个用户输入并且当前有一个动画正在运行时,通过跳到这个动画的结尾来“跳过”这个动画。

最简单的解决方案是只使用前一个动画的结束值作为新动画的开始值,但我想抽象出当前执行的动画的“类型”;它不一定是缩放动画,也可以是滚动、平移等动画。

那么有没有可能,没有进一步了解动画(我只有一个指向QPropertyAnimation的指针),使其立即跳到结尾 ?

目前,我的代码如下所示:

class View : ...
{ 
    // Property I want to animate using the mouse wheel
    Q_PROPERTY(qreal scale READ currentScale WRITE setScale)
    ...
private:
    // Pointer to the animation, which can also be another animation!
    QPropertyAnimation *viewAnimation;
}
void View::wheelEvent(QWheelEvent *e)
{
    qreal scaleDelta = pow(1.002, e->delta());
    qreal newScale = currentScale() * scaleDelta;

    if(viewAnimation)
    {
        // --- CODE SHOULD BE INSERTED HERE ---
        // --- Jump to end of viewAnimation ---
        // --- rather than canceling it     ---
        cancelViewAnimation();
    }
    viewAnimation = new QPropertyAnimation(this, "scale", this);
    viewAnimation->setStartValue(currentScale());
    viewAnimation->setEndValue(newScale);
    viewAnimation->setDuration(100);
    connect(viewAnimation, SIGNAL(finished()), SLOT(cancelViewAnimation()));
    viewAnimation->start();
}

void View::cancelViewAnimation()
{
    if(viewAnimation)
    {
        viewAnimation->stop();
        viewAnimation->deleteLater();
        viewAnimation = NULL;
    }
}

【问题讨论】:

    标签: c++ qt animation


    【解决方案1】:

    我认为这样做可以:

    if(viewAnimation)
    {
        // jump to end of animation, without any further knowledge of it:
        viewAnimation->targetObject()->setProperty(viewAnimation->propertyName(),
                                                   viewAnimation->endValue());
        cancelViewAnimation();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 2019-03-29
      • 2022-11-22
      • 1970-01-01
      相关资源
      最近更新 更多