本页内容

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”的属性。您可以通过properties 或property 指定自定义属性来覆盖此行为。

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

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

另请参阅 《 Qt Quick 中的动画和过渡》以及《Qt 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 在Transition 或Behavior 中定义,则该值默认为Transition 起始状态中定义的值,或者在Behavior 被触发时该属性的当前值。

另请参阅 Qt Quick 中的“动画与过渡”部分。

to : real

该属性用于存储动画的结束值。

如果RotationAnimation 是在Transition 或Behavior 中定义的,则该值默认为Transition 的end状态中定义的值,或者为触发Behavior 的属性change的值。

另请参阅 Qt Quick 中的“动画与过渡”。

© 2026 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.