【发布时间】:2021-10-16 20:55:47
【问题描述】:
为什么在第一种情况下左侧的动画不起作用?
如果你转到第二种情况并用 when 注释掉代码,那么动画就会起作用。如果禁用顺序动画也会出现同样的问题。
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
id: root
property int type: 0
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MouseArea {
anchors.horizontalCenter: parent.horizontalCenter
width: 50
height: 50
Rectangle { anchors.fill: parent; color: "blue" }
onClicked: {
root.type = !root.type
// stG.state = root.type ? "right" : "left" // uncomment to the second case
}
}
Rectangle {
id: switcher1
width: 50
height: 50
color: "red"
anchors.verticalCenter: parent.verticalCenter
}
Rectangle {
id: switcher2
width: 50
height: 50
color: "green"
anchors.top: switcher1.bottom
anchors.topMargin: 10
}
StateGroup {
id: stG
states: [
State {
name: "left"
when: type === 0 // comment to the second case
PropertyChanges {
target: switcher1
x: 0
}
PropertyChanges {
target: switcher2
x: 0
}
},
State {
name: "right"
when: type === 1 // comment to the second case
PropertyChanges {
target: switcher1
x: root.width - switcher1.width
}
PropertyChanges {
target: switcher2
x: root.width - switcher2.width
}
}
]
transitions: [
Transition {
to: "left"
SequentialAnimation {
NumberAnimation {
target: switcher2
property: "x"
duration: 500
}
NumberAnimation {
target: switcher1
property: "x"
duration: 500
}
}
},
Transition {
to: "right"
SequentialAnimation {
NumberAnimation {
target: switcher1
property: "x"
duration: 500
}
NumberAnimation {
target: switcher2
property: "x"
duration: 500
}
}
}
]
}
}
够了
【问题讨论】:
-
嗯....什么?你为什么不尝试描述你想要发生的事情,而不是用“一条鱼,两条鱼”来填充文本来绕过 SO 的指导方针?指南的存在是有原因的。
-
您有任何问题,或者您只是不喜欢关于鱼的文字吗?
-
是的,就像我说的:你想要发生什么?
-
添加了 gif 动画。在第一种情况下,返回动画不起作用。在第二种情况下,一切都很好
-
动画有助于让您的问题更加清晰。你仍然应该删除关于鱼的文字。