是否可以在 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 个状态,例如A1
、和B1
,并定义一个 和 之间的转换,看起来像,另一个转换 和 之间,看起来像。
但是我想知道在不引入新状态的情况下是否可以实现这样的事情。A2
B2
A1
B1
trans1
A2
B2
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" }
}