RotationAnimation QML Type

动画显示旋转值的变化。更多

Import Statement: import QtQuick
Inherits:

PropertyAnimation

属性

详细说明

RotationAnimation 是一个专门的PropertyAnimation ,用于控制动画中的旋转方向。

默认情况下,它按照数值变化的方向旋转;从 0 旋转到 240 将顺时针旋转 240 度,而从 240 旋转到 0 将逆时针旋转 240 度。可以设置direction 属性来指定旋转的方向。

在下面的示例中,我们使用 RotationAnimation 通过最短路径为状态间的旋转制作动画:

import QtQuick

Item {
    width: 300; height: 300

    Rectangle {
        id: rect
        width: 150; height: 100; anchors.centerIn: parent
        color: "red"
        antialiasing: true

        states: State {
            name: "rotated"
            PropertyChanges { target: rect; rotation: 180 }
        }

        transitions: Transition {
            RotationAnimation { duration: 1000; direction: RotationAnimation.Counterclockwise }
        }
    }

    MouseArea { anchors.fill: parent; onClicked: rect.state = "rotated" }
}

请注意,RotationAnimation 不需要设置target 值。为了方便起见,在过渡效果中使用时,RotationAnimation 将旋转所有名为 "rotation "或 "angle "的属性。您可以通过propertiesproperty 提供自己的属性来覆盖这一功能。

此外,请注意Rectangle 将围绕其默认的transformOrigin (即Item.Center )旋转。若要使用不同的变换原点,请在PropertyChanges 对象中设置原点,并在动画开始时使用PropertyAction 进行更改。详情请参见PropertyAction 文档。

与其他动画类型一样,旋转动画可以多种方式应用,包括转场、行为和属性值源。 Qt Quick 文档 中的"动画和过渡 "显示了多种创建动画的方法。

另请参阅 Qt QuickQt Quick 示例 - 动画中的动画和过渡

属性文档

direction : enumeration

该属性表示旋转的方向。

可能的值有

常量说明
RotationAnimation.Numerical(默认)通过对两个数字进行线性插值来旋转。从10 旋转到350 将顺时针旋转 340 度。
RotationAnimation.Clockwise在两个数值之间顺时针旋转
RotationAnimation.Counterclockwise在两个数值之间逆时针旋转
RotationAnimation.Shortest沿产生最短动画路径的方向旋转。从10 旋转到350 将使20 逆时针旋转几度。

from : real

该属性保存动画的起始值。

例如,在angle 值达到 100 时,才会应用下面的动画:

Item {
    states: [
        // ...
    ]

    transitions: Transition {
        RotationAnimation { properties: "angle"; from: 100; duration: 2000 }
    }
}

如果RotationAnimation 定义在TransitionBehavior 中,则该值默认为Transition 的起始状态中定义的值,或Behavior 被触发时该属性的当前值。

另请参阅 Qt Quick 中的动画和过渡


to : real

该属性保存动画的结束值。

如果RotationAnimation 定义在TransitionBehavior 中,则该值默认为Transition 的结束状态中定义的值,或触发Behavior 的属性变化值。

另请参阅 Qt Quick 中的动画和过渡


© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.