本页内容

C

Transition QML Type

定义在状态变化时触发的动画过渡效果。更多...

Import Statement: import QtQuick
Since: Qt Quick Ultralite 1.0

属性

详细说明

过渡(Transition)定义了在State 发生变化时应用的动画效果。

例如,以下Rectangle 有两个状态:默认状态和moved 状态。在moved 状态下,矩形的位置会变为(50, 50),该变化通过Transition使用Easing.InOutQuad 实现动画效果。这直观地表明矩形的状态已从默认状态变为moved 。

import QtQuick 2.15

Rectangle {
    id: rect
    width: 100; height: 100
    color: "red"

    MouseArea {
        id: mouseArea
        anchors.fill: parent
    }

    states: State {
        name: "moved"; when: mouseArea.pressed
        PropertyChanges { target: rect; x: 50; y: 50 }
    }

    transitions: Transition {
        NumberAnimation { properties: "x,y"; easing.type: Easing.InOutQuad }
    }
}

请注意,该示例未为NumberAnimation 指定to 和from 的值。为方便起见,这些属性会在状态变化前后自动设置为x 和y 的值;from 的值由x 和y 的当前值提供,而to 的值则由PropertyChanges 对象提供。若需要,您仍可指定to 和from 的值以覆盖默认值。

默认情况下,过渡效果的动画会在父项的任何状态变化时应用。可设置过渡的 `from ` 和 `to ` 值,以限制动画仅在从某个特定状态切换到另一个特定状态时应用。

要定义多个Transition,请将Item::transitions 指定为一个列表:

    transitions: [
        Transition {
            from: "*"; to: "middleRight"
            NumberAnimation {
                properties: "x,y";
                easing.type: Easing.InOutQuad;
                duration: 2000;
            }
        },
        Transition {
            from: "*"; to: "bottomLeft";
            NumberAnimation {
                properties: "x,y";
                easing.type: Easing.InOutQuad;
                duration: 200;
            }
        },
        //If any other rectangle is clicked, the icon will return
        //to the start position at a slow speed.
        Transition {
            from: "*"; to: "*";
            NumberAnimation {
                easing.type: Easing.OutExpo;
                properties: "x,y";
                duration: 4000;
            }
        }
    ]

如果指定了多个“Transition”,则对于任何特定的状态变化,仅应用其中一个(最匹配的)“Transition”。在上例中,如果矩形进入的状态既非"middleRight" 也非"bottomLeft" ,则执行第三个“Transition”,即图标被移动到起始点。

如果某个状态变化对应的过渡与Behavior 匹配的属性相同,则该过渡动画将覆盖该状态变化对应的Behavior 。

另请参阅 “动画与过渡”、“使用状态”以及“状态与过渡”。

属性文档

animations : list<Animation> [default]

该属性保存了此过渡效果将要运行的动画列表。

Item {
    transitions: Transition {
        PropertyAnimation { duration: 3000 }
        ColorAnimation { duration: 3000 }
    }
}

顶级动画将并行执行。若要按顺序执行它们,请在SequentialAnimation 中定义它们:

Item {
    transitions: Transition {
        SequentialAnimation {
            PropertyAnimation { duration: 3000 }
            ColorAnimation { duration: 3000 }
        }
    }
}

from : string

to : string

这些属性表示触发过渡的状态变化。

这些属性的默认值是“*”(即任何状态)。

例如,以下过渡未设置to 和from 属性,因此当两个状态之间发生切换时(即按下和释放鼠标时),动画始终会被应用。

Rectangle {
    id: rect
    width: 100; height: 100
    color: "red"

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

    states: State {
        name: "brighter"; when: mouseArea.pressed
        PropertyChanges { target: rect; color: "yellow" }
    }

    transitions: Transition {
        ColorAnimation { duration: 1000 }
    }
}

如果过渡效果变为如下所示:

transitions: Transition {
    to: "brighter"
    ColorAnimation { duration: 1000 }
}

则动画仅在从默认状态切换到“更亮”状态时应用(即按下鼠标时,而非松开鼠标时)。

可以通过逗号分隔的字符串设置多个to 和from 值。

在某些 Qt 许可证下可用。
了解更多。