【PySide2学习笔记】6_States and Transitions

添加 States 和 Transitions 来实现:将文本移动到屏幕底部,旋转,并在单击时更改其颜色


States_and_Transitions_animation.gif
import QtQuick 2.0

Rectangle {
    id: page
    width: 500; height: 200
    color: "lightgray"

    Text {
        id: helloText
        text: "Hello world!"
        y: 30
        anchors.horizontalCenter: page.horizontalCenter
        font.pointSize: 24; font.bold: true

        MouseArea { id: mouseArea; anchors.fill: parent }

        states: State {
            // Pressing the MouseArea activates this new state and releasing it deactivates the state.
            name: "down"; when: mouseArea.pressed == true
            // The down state includes a set of property changes from the implicit default state
            // set the y property of the text to 160, rotation to 180, and color to red
            PropertyChanges { target: helloText; y: 160; rotation: 180; color: "red" }
        }
        // add a transiton between the two states so that switching between these two states look smooth and nice
        transitions: Transition {
            // define the states between which the transition will run
            // To have a similar transition effect when changing back from the down state to the default state, 
            // set the reversible property to true
            from: ""; to: "down"; reversible: true
            // The ParallelAnimation element makes sure that 
            // the two types of animations (number and color) start at the same time.
            // You could also run them one after the other by using SequentialAnimation instead.
            ParallelAnimation {
                NumberAnimation { properties: "y,rotation"; duration: 500; easing.type: Easing.InOutQuad }
                ColorAnimation { duration: 500 }
            }
        }
    }

    Grid {
        id: colorPicker
        x: 4; anchors.bottom: page.bottom; anchors.bottomMargin: 4
        rows: 2; columns: 3; spacing: 3

        Cell { cellColor: "red"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "green"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "blue"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "yellow"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "steelblue"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "black"; onClicked: helloText.color = cellColor }
    }
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容