QML中的SmoothedAnimation平滑动画

本文介绍了如何在QML中使用SmoothedAnimation创建平滑跟随鼠标的动画效果。通过实例代码展示了如何设置动画,使得红色矩形在单击后能持续跟踪鼠标位置,提供了一个直观的交互体验。
摘要由CSDN通过智能技术生成

平滑动画是numberAnimation的子类,只是默认的easing.type被做了改动而已。

现在要做的是单击鼠标以后,一个红色的矩形框可以一直跟随鼠标。

代码来自Qt Quick核心编程这本书 作者安晓辉

代码如下:


import QtQuick 2.3
import QtQuick.Window 2.2

Window {
    visible: true
    width: 300
    height: 300

    MouseArea {
        anchors.fill: parent
        onClicked: {
            followAnimationy.from=follow.y
            followAnimationy.to=mouseY+4
            followAnimationy.start()
            followAnimationx.from=follow.x
            followAnimationx.to=mouseX+4
            followAnimationx.start()
        }
    }

    Rectangle{
    id:follow
    x:0
    y:0
    width: 100
    height: 100
    color: "red"
    }
    SmoothedAnimation{
    id:followAnimationy
    target: follow
    property: "y"
    duration: 3000

    }
    SmoothedAnimation{
    id:followAnimationx
    targe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值