赞
踩
Rectangle {
Rectangle {
id: stop_light
...
}
Rectangle {
id: go_light
...
}
states: [
State {
name: "stop"
PropertyChanges {
target: stop_light; color: "red"
}
PropertyChanges {
target: go_light; color: "black"
}
},
State {
name: "go"
PropertyChanges {
target: stop_light; color: "black"
}
PropertyChanges {
target: go_light; color: "green"
}
}
]//定义两个状态分别为go和stop,
//使用PropertyChanges为每个状态设置目标和起对应的属性值
state: "stop"//define initial state
MouseArea {
anchors.fill: parent
onClicked: parent.state == "stop" ?
parent.state = "go" :
parent.state = "stop"
}//使用MouseArea的事件响应来完成不同状态的切换
}
Rectangle {
TextInput {
id: text_field
text: "Enter text..."
}
Image {
id: clear_button
source: "images/clear.png"
MouseArea {
anchors.fill: parent
onClicked: text_field.text = ""
}
}
states:[
State {
name: "with text"
when: text_field.text != ""
PropertyChanges {
target: clear_button
opacity: 1.0
}
},
State {
name: "without text"
when: text_field.text == ""
PropertyChanges {
target: clear_button
opacity: 0.25
}
PropertyChanges {
target: text_field
focus: true
}
}
]
}
transitions: [
Transition {
from: "stop"; to: "go"
PropertyChanges {
target: stop_light
properties: "color"
duration: 1000
}
},
Transition {
from: "go"; to: "stop"
PropertyAnimation {
target: go_light
properties: "color"
duration: 1000
}
}
]
通过设置transitions属性来定义多个Transition,设置“stop”和“go”之间的状态切换
transitions: [
Transition {
from: "*"; to: "*"
PropertyChanges {
target: stop_light
properties: "color"
duration: 1000
}
PropertyChanges {
target: go_light
properties: "color"
duration: 1000
}
}
]
使用“*”匹配所有状态(默认值为“*”);只要有状态改变,过渡就会被执行;两个目标的过渡是同时进行的
transitions: [
Transition {
from: "with text"; to: "without text"
reversible: true
PropertyChanges {
target: clear_button
properties: "opacity"
duration: 1000
}
}
]
状态若使用when属性,就可以使用可逆过渡;当两个过渡应用到相同的属性时,可以采用;从状态“with text”到“without text”的时候,过渡生效,当满足条件时,再转换回来,而无须定义两个过渡
Rectangle {//Example of drop-and-bounce effect on an image
id: rect
width: 120; height: 200
Image {
id: img
source: "images/home01.jpg"
x: 60-img.width/2; y: 0
SequentialAnimation on y {
running: true
loops: Animation.Infinite
NumberAnimation {
to: 200-img.height
easing.type: "OutBounce"
duration: 2000
}
PauseAnimation {
duration: 1000
}
NumberAnimation {
to: 0
easing.type: "OutQuad"
duration: 1000
}
}
}
}
示例二:
Rectangle {//Animation as a separate element; refered to by its id
id: rect
width: 120; height: 200
PropertyAnimation {
id: animation
target: image
property: "scale"
from: 1; to: .5
}
Image {
id: image
source: "images/home01.jpg"
MouseArea {
anchors.fill: parent
onClicked: animation.start()
}
}
}
Rectangle {
...
Rectangle {
id: redRect
color: "red"
width: 100; height: 100
x:...
Behavior on x {
NumberAnimation {
duration: 300; easing.type: "InOutQuad"
}
}
}
}
Timer {
interval: 500
running: true
repeat: true
onTriggered: time.text = Date().toString()
}
Text {
id: time
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。