本页内容

SpringAnimation QML Type

允许属性以弹簧般的运动方式追踪一个值。更多...

Import Statement: import QtQuick
Inherits:

NumberAnimation

属性

详细说明

SpringAnimation 模拟了弹簧的振荡行为,通过适当的spring 常数来控制加速度,并通过damping 来控制效果衰减的速度。

您还可以限制动画的最大velocity 值。

以下Rectangle 在鼠标点击时,会使用SpringAnimation移动到鼠标所在的位置。对x 和y 值使用Behavior ,表示每当这些值发生变化时,就应应用SpringAnimation。

import QtQuick

Item {
    width: 300; height: 300

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

        Behavior on x { SpringAnimation { spring: 2; damping: 0.2 } }
        Behavior on y { SpringAnimation { spring: 2; damping: 0.2 } }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: (mouse)=> {
            rect.x = mouse.x - rect.width/2
            rect.y = mouse.y - rect.height/2
        }
    }
}

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

另请参阅 SmoothedAnimation 、 Qt Quick 中的“动画与过渡”、Qt Quick 示例 - 动画,以及Qt Quick 演示 - 时钟。

属性文档

damping : real

该属性存储弹簧阻尼值。

该值描述了弹簧状运动停止所需的时间。默认值为 0。

有效数值范围为 0 至 1.0。数值越小,停稳速度越快。

epsilon : real

该属性存储弹簧的ε值。

ε 表示该值的变化速率和幅度,其数值足够接近 0,以至于可被视为等于零。具体数值取决于该值的具体用途。对于像素位置,0.25 即可满足需求;对于缩放,0.005 即可满足需求。

默认值为 0.01。调整此值可带来微小的性能提升。

mass : real

该属性存储被移动属性的“质量”。

默认值为 1.0。

质量越大,物体的移动速度越慢,且当物体停止运动时,其弹簧般的运动幅度也越大。

modulus : real

该属性存储模数值。默认值为 0。

设置modulus 会强制目标值在模数处“循环”。例如,将模数设置为360时,值370将循环回10。

spring : real

该属性描述了目标被源吸引的强度。默认值为 0(即禁用弹簧般的运动)。

有效值范围为 0 - 5.0。

当设置此属性且velocity 的值大于0时,velocity 会限制最大速度。

velocity : real

该属性用于指定跟踪声源时允许的最大速度。

默认值为 0(无最大速度)。

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