2012-07-14 135 views
1

我使用QPropertyAnimation为用户输入设置动画以在小部件中导航。也就是说,我使用它来平滑使用鼠标滚轮进行缩放。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 % 

我恨APPLICAT在动画结束之前无法进行更多用户输入的离子,因此我主要讨厌流畅的动画。所以我想要的是:当用户给出另一个用户输入并且当前有一个动画正在运行时,通过跳到这个动画的末尾来“跳过”这个动画。

最简单的解决方案是将刚才的动画的结束值用于新动画的开始值,但我想抽象当前正在执行的动画的“类型”它不一定是缩放动画,但也可以是滚动,平移,任何动画。

那么,有没有一种可能性,没有动画的进一步了解(我只有一个指向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; 
    } 
} 

回答

2

我想这会做到这一点:

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