QML - 为什么动画会发生冲突?

时间:2018-02-17 09:00:46

标签: qt animation qml

Qml - Flipable的示例:

import QtQuick 2.0

Flipable {
    id: flipable
    width: 240
    height: 240

    property bool flipped: false

    front: Rectangle { width: 200; height: 200; color: 'red'; anchors.centerIn: parent }
    back: Rectangle { width: 200; height: 200; color: 'blue'; anchors.centerIn: parent }

    transform: Rotation {
        id: rotation
        origin.x: flipable.width/2
        origin.y: flipable.height/2
        axis.x: 0; axis.y: 1; axis.z: 0     // set axis.y to 1 to rotate around y-axis
        angle: 0    // the default angle
    }

    states: State {
        name: "back"
        PropertyChanges { target: rotation; angle: 180 }
        when: flipable.flipped
    }

    transitions: Transition {
        NumberAnimation { target: rotation; property: "angle"; duration: 4000 }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: flipable.flipped = !flipable.flipped
    }
}

此示例运行正常,但如果我使用此代码,则Flipable无法运行:

MouseArea {
  anchors.fill: parent
  onClicked: {
    flipable.flipped = true;
    flipable.flipped = false;
  }
}

我认为当我第一次使flipped属性为true然后false时,动画会出现问题。我希望flipable打开然后关闭。

1 个答案:

答案 0 :(得分:0)

问题是您在翻转动画开始之前将属性flipped设置回false

如果你想要完整的开/关动画,你必须等待"打开"动画在开始之前完成"关闭"动画:

transitions: Transition {
    id: transition
    onRunningChanged: {
        if (!running && flipable.flipped) {
            flipable.flipped = false;
        }
    }
    NumberAnimation { target: rotation; property: "angle"; duration: 4000 }
}

MouseArea {
    anchors.fill: parent
    onClicked: {
        if (!transition.running) {
            flipable.flipped = true;
        }
    }
}