简体   繁体   English

在鼠标单击QML时重复状态

[英]Repeat state on mouse click QML

Within the code I have a State , a Transition and a RotationAnimation defined. 在代码中,我定义了StateTransitionRotationAnimation

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更改为另一个Statefrom属性更改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 . 无论如何,存在多种应用动画的方法,包括但不限于使用预定义目标直接属性动画的 BehaviorTransition 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM