相同两个状态之间的不同过渡动画

时间:2011-06-23 13:06:42

标签: qt qml qt-quick

是否可以在QML组件中的两个状态之间使用不同的过渡动画?以下示例不起作用,程序崩溃(Linux上的分段错误):

import QtQuick 1.0

Rectangle {
    id: canvas
    height: 500; width: 600

    Rectangle { id: rect; color: "#04A"; height: 100; width: 100 }

    state: "A"
    states: [
        State { name: "A"; PropertyChanges { target: rect; x: 0; y: 100 } },
        State { name: "B"; PropertyChanges { target: rect; x: 500; y: 100 } }
    ]


    transitions: trans1

    property list<Transition> trans1: [
        Transition {
            NumberAnimation { target: rect; property: "x"; duration: 500 }
        }
    ]

    property list<Transition> trans2: [
        Transition {
            from: "A"; to: "B"
            SequentialAnimation {
                NumberAnimation { target: rect; property: "x"; from: 0; to: -100; duration: 250 }
                NumberAnimation { target: rect; property: "x"; from: 600; to: 500; duration: 250 }
            }
        },
        Transition {
            from: "B"; to: "A"
            SequentialAnimation {
                NumberAnimation { target: rect; property: "x"; from: 500; to: 600; duration: 250 }
                NumberAnimation { target: rect; property: "x"; from: -100; to: 0; duration: 250 }
            }
        }
    ]


    // test script /////////////////////////////////////////////////////////

    Timer { interval: 1000; running: true; onTriggered: canvas.state = "B" }
    Timer { interval: 2000; running: true; onTriggered: canvas.state = "A" }

    // change kind of transition
    Timer { interval: 3000; running: true; onTriggered: canvas.transitions = trans2 }

    Timer { interval: 4000; running: true; onTriggered: canvas.state = "B" }
    Timer { interval: 5000; running: true; onTriggered: canvas.state = "A" }
}

QML-Doc表示transtition属性只读,但通常会将Transition{...}个元素列表分配给此属性,因此它可以不是真的只读,可以吗?

一种解决方案是使用4种状态,例如A1B1A2B2,并定义A1B1之间的转换,看起来像trans1A2B2之间的另一个转换,看起来像trans2但我想知道在不引入新状态的情况下是否可以这样做。


修改

gregschlom 更改from / to属性的建议有效,这里有一个示例:

import QtQuick 1.0

Rectangle {
    id: canvas
    height: 500; width: 600

    Rectangle { id: rect; color: "#04A"; height: 100; width: 100 }

    state: "A"
    states: [
        State { name: "A"; PropertyChanges { target: rect; x: 0; y: 100 } },
        State { name: "B"; PropertyChanges { target: rect; x: 500; y: 100 } }
    ]


    property int transType: 1

    transitions: [
        Transition {
            from: transType == 1 ? "*" : "none"
            to:   transType == 1 ? "*" : "none"
            ParallelAnimation {
                RotationAnimation { target: rect; property: "rotation"; from: 0; to:360; duration: 500 }
                NumberAnimation { target: rect; property: "x"; duration: 500 }
            }
        },
        Transition {
            from: transType == 2 ? "*" : "none"
            to:   transType == 2 ? "*" : "none"
            NumberAnimation { target: rect; property: "x"; duration: 500 }
        }
    ]


    // test script /////////////////////////////////////////////////////////

    Timer { interval: 1000; running: true; onTriggered: canvas.state = "B" }
    Timer { interval: 2000; running: true; onTriggered: canvas.state = "A" }

    // change kind of transition
    Timer { interval: 3000; running: true; onTriggered: canvas.transType = 2 }

    Timer { interval: 4000; running: true; onTriggered: canvas.state = "B" }
    Timer { interval: 5000; running: true; onTriggered: canvas.state = "A" }
}

1 个答案:

答案 0 :(得分:2)

  

以下示例不起作用,程序崩溃(Linux上的分段错误):

请向https://bugreports.qt.io报告此类崩溃事件。崩溃是最优先考虑的,通常会在几天内确定。

  

QML-Doc说transstition属性是只读的,但通常会将一个Transition {...}元素列表分配给此属性,因此它不能是真正的只读属性,是吗? / p>

认为在运行时分配(即:在脚本中)与在qml文件中分配不同,所以当说“只读”时可能意味着你不应该初始化后,更改值。

无论如何,如果医生说它是只读的,并且当你试图写它时它会崩溃,那么你知道你可能不应该这么做...... :)

  

但我想知道如果不引入新的状态,这样的事情是否可行。

关于Transitions的qml doc说:

  

如果指定了多个Transition,则只对任何特定的状态更改应用单个(最佳匹配)Transition。在上面的示例中,当更改为state1时,将使用第一个转换,而不是更通用的第二个转换。

因此,您可以尝试一次定义所有转换,并动态更改“to”和“from”设置以尝试启用/禁用转换。不确定是否可能,但它可以工作。