将元素添加到具有动画的中继器中

Add an element to a repeater with animation

本文关键字:动画 中继器 元素 添加      更新时间:2023-09-26

我有一个中继器,在创建元素时会对其进行动画处理。

Repeater {
    model : 0
    Image {
       ...
       Animation {
           ...
       }
    }
}

如果在上一个元素的动画完成后向模型中添加一个元素,则一切都正常。但是如果我之前加了一个元素,它就不起作用了。例如,如果我有一把能射出子弹的枪,如果我等到子弹的动画结束,一切都会正常工作。但如果我想在第一颗子弹结束前再射出一颗子弹,第一颗就会消失,我只看到第二颗的动画。

我应该怎么做才能看到所有的动画?

也许你的模型、代理或布局有问题(项目重叠…),因为在这里,这很好:

import QtQuick 2.0;
Rectangle {
    width: 400;
    height: 300;
    Timer {
        running: true;
        repeat: true;
        interval: 1000;
        onTriggered: { modelTest.append ({ "bg" : Qt.hsla (Math.random (), 0.85, 0.45, 1.0).toString () }); }
    }
    Flow {
        anchors.fill: parent;
        Repeater {
            model: ListModel {
                id: modelTest;
            }
            delegate: Rectangle {
                id: rect;
                color: model.bg;
                width: 50;
                height: width;
                scale: 0.0;
                PropertyAnimation {
                    target: rect;
                    property: "scale";
                    from: 0.0;
                    to: 1.0;
                    duration: 450;
                    running: true;
                    loops: 1;
                }
            }
        }
    }
}

请记住,只有ListModel和QAbstractListModel能够在不重置整个委托的情况下动态添加新项,另一个(变体列表、JS数组、数字)将导致所有委托在每次修改模型时重新初始化。。。