假設沒有固定設置在我們的目標Rectangle
,你有一組選項來實現你想要的。
1.各國和轉換
在這種方法中,你只需定義一個State
:擴展的一個。其他State
是默認值,即State
,其中Rectangle
只覆蓋窗口的一小部分。 A Transition
適用於無from
和to
,因此在Rectangle
展開或縮小時應用它。由於State
s,我們不需要存儲以前的座標,因爲它們的值是恢復時當默認狀態設置回。請注意,在下面的示例中,我從x
和y
中刪除了Math.random()
,以避免每次從「EXPANDED」狀態返回時重新評估和分配隨機座標。下面是代碼:
import QtQuick 2.4
import QtQuick.Controls 1.3
ApplicationWindow {
id: win
title: qsTr("Playground")
width: 620
height: 420
visible: true
Rectangle {
id: rect
width: 20
height: 20
color: "black"
MouseArea {
anchors.fill: parent
onDoubleClicked: {
rect.state = rect.state === "EXPANDED" ? "" : "EXPANDED"
}
}
states: [
State {
name: "EXPANDED"
PropertyChanges { target: rect; x: 0}
PropertyChanges { target: rect; y: 0}
PropertyChanges { target: rect; width: parent.width}
PropertyChanges { target: rect; height: parent.height}
}
]
transitions: [
Transition {
ParallelAnimation {
NumberAnimation { target: rect; property: "x"; duration: 350 }
NumberAnimation { target: rect; property: "y"; duration: 350 }
NumberAnimation { target: rect; property: "width"; duration: 350}
NumberAnimation { target: rect; property: "height"; duration: 350}
}
}
]
// randomization is applied with JS --> no properties binding
Component.onCompleted: {
rect.x = Math.random() * 600
rect.y = Math.random() * 400
}
}
}
2.動畫
簡而言之,定義一個動畫來擴大Rectangle
和一個萎縮下來。然後根據位置/大小/其他調用一個或另一個。你可以寫這樣的事情:
import QtQuick 2.4
import QtQuick.Controls 1.3
ApplicationWindow {
id: win
title: qsTr("Playground")
width: 620
height: 420
visible: true
Rectangle {
id: rect
x: Math.random() * 600
y: Math.random() * 400
property int oldx; property int oldy
width: 20
height: 20
color: "black"
MouseArea {
anchors.fill: parent
onDoubleClicked: {
if(rect.x === 0){
shrink.start()
} else {
rect.oldx = rect.x
rect.oldy = rect.y
expand.start()
}
}
}
ParallelAnimation {
id: shrink
NumberAnimation { target: rect; property: "x"; to: rect.oldx; duration: 350 }
NumberAnimation { target: rect; property: "y"; to: rect.oldy; duration: 350 }
NumberAnimation { target: rect; property: "width"; to: 20; duration: 350}
NumberAnimation { target: rect; property: "height"; to: 20; duration: 350}
}
ParallelAnimation {
id: expand
NumberAnimation { target: rect; property: "x"; to: 0; duration: 350 }
NumberAnimation { target: rect; property: "y"; to: 0; duration: 350 }
NumberAnimation { target: rect; property: "width"; to: win.width; duration: 350}
NumberAnimation { target: rect; property: "height"; to: win.height; duration: 350}
}
}
}
3.行爲
一個Behavior
定義屬性更改默認的動畫。在這種方法中,我們爲所涉及的不同性質定義了一組動畫(x
,y
,width
和height
)。通過這種方式,我們只需要將屬性更新爲正確的值,然後在Behavior
之間執行動畫更改的任務。你可以寫這樣的事情:
import QtQuick 2.4
import QtQuick.Controls 1.3
ApplicationWindow {
id: win
title: qsTr("Playground")
width: 620
height: 420
visible: true
Rectangle {
id: rect
x: Math.random() * 600
y: Math.random() * 400
property int oldx; property int oldy
width: 20
height: 20
color: "black"
MouseArea {
anchors.fill: parent
onDoubleClicked: {
if(rect.x === 0){
rect.x = rect.oldx
rect.y = rect.oldy
rect.width = rect.height = 20
} else {
rect.oldx = rect.x
rect.oldy = rect.y
rect.x = rect.y = 0
rect.width = win.width
rect.height = win.height
}
}
}
Behavior on x {
NumberAnimation { duration: 450 }
}
Behavior on y {
NumberAnimation { duration: 450 }
}
Behavior on width {
NumberAnimation { duration: 450 }
}
Behavior on height {
NumberAnimation { duration: 450 }
}
}
}
第一種方法是,到目前爲止,所有的清潔解決方案,因爲它不涉及臨時變量的使用,如第二和第三。 State
s隨着組件在它們之間移動,自動保存/恢復變量的狀態。
這將取決於如何定位矩形。換句話說,你在使用座標和尺寸,還是你在使用錨? – MrEricSir