在不同属性上分组动画

时间:2016-05-25 15:36:03

标签: qt animation qml

我在QML中有一个通用的QtObject我想要动画属性,我想一起开始/停止它们。我编写了以下代码,但是Qt Creator错误:qrc:/DummyData.qml:11 Cannot assign to non-existent default property

...第11行是ParallelAnimation {的那个。

import QtQuick 2.5

QtObject {
  property real windowFrontLeftOpenPct:  0.0
  property real windowFrontRightOpenPct: 0.0
  property real windowRearLeftOpenPct:   0.0
  property real windowRearRightOpenPct:  0.0

  property bool windowAnimationsActive: false

  ParallelAnimation {
    running: windowAnimationsActive

    SequentialAnimation on windowFrontLeftOpenPct {
      loops: Animation.Infinite
      NumberAnimation { from:0; to:1; duration:2000; easing.type:Easing.InOutSine }
      NumberAnimation { from:1; to:0; duration:2000; easing.type:Easing.InOutSine }
    }
    SequentialAnimation on windowFrontRightOpenPct {
      loops: Animation.Infinite
      NumberAnimation { from:0; to:1; duration:2000; easing.type:Easing.InOutSine }
      NumberAnimation { from:1; to:0; duration:2000; easing.type:Easing.InOutSine }
    }
    SequentialAnimation on windowRearLeftOpenPct {
      loops: Animation.Infinite
      NumberAnimation { from:0; to:1; duration:2000; easing.type:Easing.InOutSine }
      NumberAnimation { from:1; to:0; duration:2000; easing.type:Easing.InOutSine }
    }
    SequentialAnimation on windowRearRightOpenPct {
      loops: Animation.Infinite
      NumberAnimation { from:0; to:1; duration:2000; easing.type:Easing.InOutSine }
      NumberAnimation { from:1; to:0; duration:2000; easing.type:Easing.InOutSine }
    }
  }
}

我是否可以在单个并行动画中混合使用不同的目标属性?

注意:在这种情况下,解决方法是删除ParallelAnimation包装器并将running:windowAnimationsActive应用于每个SequentialAnimations。但是,我认为这不是一个通用的解决方案,因为它不允许我通过单个父包装器轻松地暂停和恢复许多动画。

1 个答案:

答案 0 :(得分:2)

只需将其存储在自定义属性中:

QtObject {
    id: windowPositions
    property real windowFrontLeftOpenPct:  0.0
    property real windowFrontRightOpenPct: 0.0
    property real windowRearLeftOpenPct:   0.0
    property real windowRearRightOpenPct:  0.0

    property bool windowAnimationsActive: false

    property ParallelAnimation windowAnimations: ParallelAnimation {
        running: windowAnimationsActive

        SequentialAnimation {
            loops: Animation.Infinite
            NumberAnimation { target: windowPositions; property: "windowFrontLeftOpenPct"; from:0; to:1; duration:2000; easing.type:Easing.InOutSine }
            NumberAnimation { target: windowPositions; property: "windowFrontLeftOpenPct"; from:1; to:0; duration:2000; easing.type:Easing.InOutSine }
        }
        SequentialAnimation {
            loops: Animation.Infinite
            NumberAnimation { target: windowPositions; property: "windowFrontRightOpenPct"; from:0; to:1; duration:2000; easing.type:Easing.InOutSine }
            NumberAnimation { target: windowPositions; property: "windowFrontRightOpenPct"; from:1; to:0; duration:2000; easing.type:Easing.InOutSine }
        }
        SequentialAnimation {
            loops: Animation.Infinite
            NumberAnimation { target: windowPositions; property: "windowRearLeftOpenPct"; from:0; to:1; duration:2000; easing.type:Easing.InOutSine }
            NumberAnimation { target: windowPositions; property: "windowRearLeftOpenPct"; from:1; to:0; duration:2000; easing.type:Easing.InOutSine }
        }
        SequentialAnimation {
            loops: Animation.Infinite
            NumberAnimation { target: windowPositions; property: "windowRearRightOpenPct"; from:0; to:1; duration:2000; easing.type:Easing.InOutSine }
            NumberAnimation { target: windowPositions; property: "windowRearRightOpenPct"; from:1; to:0; duration:2000; easing.type:Easing.InOutSine }
        }
    }
}

请注意,您不能以这种方式使用'on property'语法。哟需要按目标和名称指定。您可以使用properties通过一个动画更改多个:

QtObject {
    id: windowPositions
    property real windowFrontLeftOpenPct:  0.0
    property real windowFrontRightOpenPct: 0.0
    property real windowRearLeftOpenPct:   0.0
    property real windowRearRightOpenPct:  0.0

    property bool running: false

    property SequentialAnimation windowAnimations: SequentialAnimation {
            running: windowPositions.running
            loops: Animation.Infinite

            NumberAnimation { target: windowPositions;
                properties: "windowFrontLeftOpenPct,windowFrontRightOpenPct,windowRearLeftOpenPct,windowRearRightOpenPct";
                from:0; to:1; duration:2000; easing.type:Easing.InOutSine }
            NumberAnimation { target: windowPositions;
                properties: "windowFrontLeftOpenPct,windowFrontRightOpenPct,windowRearLeftOpenPct,windowRearRightOpenPct";
                from:1; to:0; duration:2000; easing.type:Easing.InOutSine }
    }
}

这只是一个概念,因为为同一个值创建4个相同的属性是没用的,因为在这里创建QtObject是无用的,如果你需要它,我会称之为糟糕的设计。