更改动画目标

Vel*_*kan 6 qt qml qtquick2

无法将动画从一个对象切换到另一个对象.id更改(它在日志中打印'world'),但它不传输动画:hello仍然闪烁并且world是静态的.

它只在呼叫时才能正常工作a.restart().当没有功能,只有绑定时,您可以使用onChanged和控制动画停止(完成或暂停)的方式if (running) { complete(); restart(); }.

import QtQuick 2.5

Column {
    ColorAnimation {
        id: a

        target: lab1
        property: "color"

        running: true
        loops: Animation.Infinite
        duration: 500

        from: "black"
        to: "red"
    }

    Text {
        id: lab1

        text: "hello"

        MouseArea {
            anchors.fill: parent
            onClicked: {
                a.target = lab2
                console.log("changed")
                console.log(a.target.text)
            }
        }
    }

    Text {
        id: lab2

        text: "world"
    }
}
Run Code Online (Sandbox Code Playgroud)

Vel*_*kan 1

我现在将使用它(刚刚添加 onTargetChanged):

import QtQuick 2.5

Column {
    ColorAnimation {
        id: a

        target: lab1

        onTargetChanged: {
            if (running) { complete(); restart(); }
        }

        property: "color"

        running: true
        loops: Animation.Infinite
        duration: 500

        from: "black"
        to: "red"
    }

    Text {
        id: lab1

        text: "hello"

        MouseArea {
            anchors.fill: parent
            onClicked: {
                a.target = lab2
                console.log("changed")
                console.log(a.target.text)
            }
        }
    }

    Text {
        id: lab2

        text: "world"
    }
}
Run Code Online (Sandbox Code Playgroud)

并通过绑定(按下时动画切换到另一个标签):

import QtQuick 2.5

Column {
    id: root

    ColorAnimation {
        id: a

        target: ma.pressed ? lab2 : lab1

        onTargetChanged: {
            if (running) { complete(); restart(); }
        }

        property: "color"

        running: true
        loops: Animation.Infinite
        duration: 500

        from: "black"
        to: "red"
    }

    Text {
        id: lab1

        text: "hello"

        MouseArea {
            id: ma
            anchors.fill: parent
        }
    }

    Text {
        id: lab2

        text: "world"
    }
}
Run Code Online (Sandbox Code Playgroud)