qml分组动画

这里介绍2种分组动画,一种是顺序执行(SequentialAnimation),即:若其中放有若干个子动画,则执行完第一个然后继续执行第二个以此类推。

还有一种是并行执行(ParallelAnimation),即:若其中放有若干个子动画,则不分先后顺序,一起同步执行。

下面看一个例子,目标:飞机从左下角待命,当我们点击飞机图片时,飞机立即开始飞行,从从左下角飞到左上角,掉头然后从左上角飞到右上角,掉头然后从右上角飞到右下角,掉头然后从右下角飞到左下角,最后掉头回到初始位置,再次点击飞机,则会再飞行一圈。

main.qml 

import QtQuick

Window {
    id: root
    width: 640
    height: 480
    visible: true
    title: qsTr("分组动画演示")

    Fly {
        id: fly
        source: "image/飞机.png"
        x: 70
        y: 400

        onClicked: sam.restart()
    }

    property int duration: 3000

    // 顺序执行动画,而ParallelAnimation是并行执行动画
    SequentialAnimation {
        id: sam

        // 飞机从左下角移动到左上角
        NumberAnimation {
            target: fly //指定是上面的飞机图片
            properties: "y" //目标值是y
            from: 400
            to: 80
            duration: root.duration
        }


        NumberAnimation {
            target: fly
            property: "rotation"
            duration: root.duration
            from: 0
            to: 90
        }

        NumberAnimation {
            target: fly
            property: "x"
            duration: root.duration
            from: 70
            to: 500
        }

        NumberAnimation {
            target: fly
            property: "rotation"
            duration: root.duration
            from: 90
            to: 180
        }

        NumberAnimation {
            target: fly
            properties: "y"
            from: 80
            to: 400
            duration: root.duration
        }

        NumberAnimation {
            target: fly
            property: "rotation"
            duration: root.duration
            from: 180
            to: 270
        }

        NumberAnimation {
            target: fly
            property: "x"
            duration: root.duration
            from: 500
            to: 70
        }

        NumberAnimation {
            target: fly
            property: "rotation"
            duration: root.duration
            from: 270
            to: 360
        }

        //重置飞机的位置,让飞机回到起始位置
        ScriptAction {
            script: {
                fly.x = 70
                fly.y = 400
                fly.rotation = 0
            }
        }
    }

    // Text {
    //     id: fly_state
    //     text: qsTr("准备起飞")
    //     font.pixelSize: 16
    //     anchors.centerIn: parent
    // }

    // PropertyAnimation {
    //     target: fly_state
    //     property: "text"
    //     to: "飞机正在向前飞行…"
    //     duration: root.duration
    // }
}

Fly.qml

import QtQuick

Item {
    id: root

    property alias source: fly.source //图片路径开放
    // property alias fly_state: fly_state.text
    signal clicked

    Image {
        id: fly

        MouseArea {
            anchors.fill: parent
            onClicked: root.clicked()
        }
    }
}

 这里我想着飞机在飞行时,程序框中心也会实时更新出文字,用于描述飞机目前飞行的状态,但是没有尝试成功,暂不在目前知识范围内。。。。后面我会回来加上,嘿嘿!

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵呜角角

如果对你有所帮助,哪怕1毛钱~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值