[英]Repeat state on mouse click QML
Within the code I have a State
, a Transition
and a RotationAnimation
defined. 在代码中,我定义了
State
, Transition
和RotationAnimation
。
After the property change takes place, ie after the rotation
is set to 360
, the onClicked
handler is rendered mute and the animation will never be played again after subsequent MouseArea
clicks. 在进行属性更改之后,即在将
rotation
设置为360
,将onClicked
处理函数呈现为静音,并且在随后的MouseArea
单击之后将不再播放动画。
Here is my current code: 这是我当前的代码:
Image{
id: logo
x: 29
source: "LSFO-IngeniiSymbol.png"
width: 70
height: 70
states: State {
name: "rotated" ; when: area.pressed
PropertyChanges {target: logo; rotation: 360 }
}
transitions: Transition {
RotationAnimation {
id: rotateanimate
duration: 1000
direction: RotationAnimation.Clockwise
}
}
MouseArea {
id: area
anchors.fill: parent
onClicked: {
logo.state = "rotated"
// pageloader.source = "HelpDoc.qml"
// pageloader.source = ""
// pageloader.source = "HelpDoc.qml"
}
}
}
State
s are a way to represent different property configurations for a given Item
. State
是表示给定Item
不同属性配置的一种方式。 Each State
has its unique set of values for the properties defined inside the specific Item
. 每个
State
在特定Item
内定义的属性都有其唯一的一组值。
Transition
s are a way to add animations to an Item
when the current State
changes to another State
. Transition
s是当当前State
更改为另一个State
时向Item
添加动画的一种方法。 They can be applied in a "restricted way", ie only when the Item
changes from a particular State
to another (see from
and to
properties), or they can be applied "globally" for each State
change. 它们可以“受限制的方式”应用,即仅当
Item
从特定State
更改为另一个State
( from
属性更改to
属性)时,或者可以针对每个State
更改“全局”应用。 In any case a change is needed . 无论如何都需要改变。
In your code the state
is set to rotated
and after that the very same State
is reassigned so that no further changes occur. 在您的代码中,
state
被设置为rotated
,然后重新分配了相同的State
,因此不会发生进一步的更改。 Hence, there is no room for another Transition
, either to rotated
state
or the default empty-named State
. 因此,没有空间可以进行另一个
Transition
,无论是rotated
state
还是默认的空名称State
。
Anyhow, there are various ways to apply animations including, but not limited to, Behavior
s , Transition
s, using predefined targets and direct property animation . 无论如何,存在多种应用动画的方法,包括但不限于使用预定义目标和直接属性动画的
Behavior
, Transition
。 It seems to me the latter is the way to go for this specific case. 在我看来,后者是解决此特定情况的方法。 Here is a revisited version of your code with the animation triggered on mouse click:
这是您的代码的重新访问版本,单击鼠标即可触发动画:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
ApplicationWindow {
width: 250
height: 250
visible: true
Image{
id: logo
anchors.centerIn: parent
source: "https://yt3.ggpht.com/-YiF6y9OXDQ8/AAAAAAAAAAI/AAAAAAAAAAA/oFNmJLmzqDA/s100-c-k-no/photo.jpg"
width: 100
height: 100
RotationAnimation {
id: rotate
target:logo
property: "rotation"
from: 0
to: 360
duration: 1000
direction: RotationAnimation.Clockwise
}
MouseArea {
id: area
anchors.fill: parent
onClicked: {
rotate.start() // animation plays here!
// your code
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.