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

问题描述 投票:0回答:1

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打开然后关闭。

qt animation qml
1个回答
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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.