【问题标题】:How to add animation to a component while destroying in QML如何在 QML 中销毁时向组件添加动画
【发布时间】:2018-04-13 10:52:05
【问题描述】:

我正在将动画添加到我的项目中,并且大部分 UI 都是动态的。目前,我无法在销毁时向Component 添加动画。以下是描述相同的测试应用程序代码:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    property bool removeClicked : true

    Row{
        anchors.bottom: parent.bottom
        spacing:20
        Button{
            text:"Add"

            onClicked: {
                removeClicked = false
                comp.createObject(myrow)
            }
        }

        Button{
            id:remBtn
            text:"Remove"

            onClicked: {
                removeClicked = true
                myrow.children[0].destroy() //Destroy the object

            }
        }
    }

    Row{
        id:myrow
        height:40
        spacing:20
    }

    Component{
        id:comp
        Rectangle{
            width:20
            height:30
            color: "red"

            NumberAnimation on opacity{
                id: destroyAnimation
                from :removeClicked ? 1:0
                to: removeClicked ? 0:1
                duration: 1000
            }
        }
    }
}

任何帮助将不胜感激!!!

【问题讨论】:

    标签: qt animation qml qtquick2 qtquickcontrols2


    【解决方案1】:

    Shou 应该在您动态创建的项目上调用destroy 之前 执行动画。您可以使用SequentialAnimation,结合ScriptAction 来执行此操作。

    这是一个小例子(动态球在点击时会被破坏)。

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
    
        Button{
            text:"Add"
            anchors.centerIn: parent
            onClicked: {
                comp.createObject(parent)
            }
        }
    
        Component{
            id:comp
            Rectangle{
                id: ball
                height:30
                width:height
                radius: height/2
                x: Math.random()*parent.width-width
                y: Math.random()*parent.height-height
                color: Qt.hsla(Math.random(), 0.5, 0.5, 1)
                opacity: 0
    
                Component.onCompleted: opacity = 1
    
                Behavior on opacity{ NumberAnimation {}}
                SequentialAnimation
                {
                    id: destroyAnim
                    ScriptAction{script: ball.opacity = 0}
                    NumberAnimation{target: ball; property:"scale"; to: 5}
                    ScriptAction{script: ball.destroy()}
                }
    
                MouseArea
                {
                    anchors.fill: parent
                    onClicked:destroyAnim.start()
                }
            }
        }
    }
    

    【讨论】:

    • 你的回答给了我很多实现动画的想法,我有一个疑问,为什么需要这条线? Behavior on opacity{ NumberAnimation {}}
    • 任何时候 opacity 值发生变化,都需要 200 毫秒(默认动画持续时间)从之前的值过渡到新的值。这会在创建时创建淡入动画,并在销毁时创建淡出。
    猜你喜欢
    • 2017-11-20
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多