gpt4 book ai didi

c++ - QPropertyAnimation : Immedately jump to end of animation?

转载 作者:行者123 更新时间:2023-11-30 04:26:36 28 4
gpt4 key购买 nike

我使用 QPropertyAnimation 为用户输入设置动画以在小部件中导航。即,我使用它来使用鼠标滚轮平滑缩放。

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

例如,如果放大操作将 View 缩放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;
}
}

最佳答案

我想这样就可以了:

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

关于c++ - QPropertyAnimation : Immedately jump to end of animation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11483744/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com