7

我正在尝试使可见的动画连续。我有一个 javascript 函数,它调用 myModel.move() 两次。我有一个 GridView 来显示 myModel,并且我有“x 上的行为”动画,因此我可以直观地看到这些动作。但是,两个运动动画都并行运行(它们之间的小延迟并不明显)。

我的想法是添加一个计数器,记录有多少动画已经开始,有多少已经完成。像这样的东西;

Behavior on x {
    NumberAnimation {
        id: animationX;
        duration: 500;
        onRunningChanged: {
            if (animationX.running) {
                console.log("Animation start");
                myModel.busy = myModel.busy + 1
            } else {
                console.log("Animation stop");
                myModel.busy = myModel.busy - 1
            }
        }
    }
}

这按预期工作。然后,我在我的 javascript 函数中添加一个循环,以等待所有动画完成。

ListModel {
    id: myModel
    property int busy: 0
    function doSomething() {
        myModel.move(...)
        while (myModel.busy) {}
        myModel.move(...)
    }
}

这是问题所在。我可以看到在第一次 move() 之后所有必要的动画都开始了,但是什么也看不到,也没有动画完成。我有某种僵局。如何解决这个问题?

4

2 回答 2

2
function doSomething() {
    myModel.move(...)
    while (myModel.busy) {}
    myModel.move(...)
}

我不擅长 javascript。但是你为什么忙循环呢?我会创建 2 个函数。

  • 第一个 make myModel.move(),然后为将来的事件准备字段(例如创建一个将被单击的隐藏按钮)
  • 第二个将在未来事件创建时调用。对于上面的示例,它将是Onclick.

这似乎onRunningChanged是事件处理程序的定义。onModelIdle为什么不创建相同的,让我们称之为

...
   onRunningChanged: {
        if (animationX.running) {
            console.log("Animation start");
            myModel.busy = myModel.busy + 1
        } else {
            console.log("Animation stop");
            myModel.busy = myModel.busy - 1
            if(myModel.busy == 0)
              // don't know the code, but trigger the onModelIdle event which 
              // be handled below
        }
    }
...

ListModel {
   id: myModel
   ...

   onModelIdle{
       myModel.move(...)
   }
}
于 2012-09-07T13:32:57.520 回答
1

这是基于#UmNyobe 的答案的有效解决方案。好吧,QML 是一种声明性语言,因此对于迭代问题是有问题的。欢迎任何意见。也许,有人可以提出更好(更易读)的纯 QML 程序,它会产生相同的效果。

import QtQuick 1.1
GridView {
    id: mainGrid
    cellWidth: 165; cellHeight: 95
    width: 5*cellWidth; height: 4*cellHeight
    model: myModel
    delegate: myButton
    property string animate: "no"
    property int busy: 0
    signal busyPlus
    signal busyMinus
    onBusyPlus: {
        busy++
    }
    onBusyMinus: {
        busy--
        if (busy == 0) mainGrid.model.algorithm()
    }
    ListModel {
        id: myModel
        property int step: 0
        property int one: 0
        function createModel() {
            for (var i=1; i<=20; i++) {
                append({"display": i})
            }
        }
        function algorithm() {
            if (step == 0) {
                move(0,19,1)
                one = 0
                step++
            }
            else if (step == 1) {
                if (one < 19) {
                    move(one,one+1,1)
                    one++
                    if (one == 19) step++
                }
            }
            else if (step == 2) {
                move(0,1,1)
                move(5,6,1)
                move(10,11,1)
                move(15,16,1)
                step++
            }
        }
        Component.onCompleted: {
            createModel()
            mainGrid.animate = "yes"
            algorithm()
        }
    }
    Component {
        id: myButton
        Item {
            id: item
            width: mainGrid.cellWidth-5; height: mainGrid.cellHeight-5;
            Rectangle {
                id: box
                parent: mainGrid
                x: item.x; y: item.y;
                width: item.width; height: item.height;
                border.width: 1
                Text {
                    anchors.centerIn: parent
                    text: display
                    font.pixelSize: 48
                }
                Behavior on x {
                    enabled: mainGrid.animate == "yes"
                    NumberAnimation {
                        id: animationX;
                        duration: 1000;
                        onRunningChanged: {
                            if (animationX.running) mainGrid.busyPlus()
                            else mainGrid.busyMinus()
                        }
                    }
                }
                Behavior on y {
                    enabled: mainGrid.animate == "yes"
                    NumberAnimation {
                        id: animationY;
                        duration: 1000;
                        onRunningChanged: {
                            if (animationY.running) mainGrid.busyPlus()
                            else mainGrid.busyMinus()
                        }
                    }
                }
            }
        }
    }
}
于 2013-01-02T14:31:56.950 回答