2016-05-17 55 views
1

以及即時嘗試使用順序動畫一次動畫多個對象。爲了我的目的,我需要從順序動畫(和數字動畫)的decleration之外訪問它。這工作正常,但只設置一個動畫時一個對象。如果我爲每個對象定義一個數字動畫,這將起作用 - 但我不想這樣做,因爲稍後我要在循環內分配動畫,動畫的大小會有所不同。所以我故意調用同一個數字動畫對象。有沒有一種方法來動畫一堆對象,而無需定義每個數字動畫?下面是一些代碼:如何在qml中動畫多個對象

Component.onCompleted: { 

    seq.access.target = rec1 
    seq.access.property = "y" 
    seq.access.to = 50 
    seq.start() 


    seq.access.target = rec2 
    seq.access.property = "y" 
    seq.access.to = 50 
    seq.start() 

} 

Rectangle{ 
    id: rec1 
    width: 50 
    height: 50 
    color: "red" 
} 
Rectangle{ 
    id: rec2 
    x: 100 
    width: 50 
    height: 50 
    color: "blue" 
} 

SequentialAnimation{ 
    id: seq 
    property alias access: num 
    NumberAnimation{id: num} 
} 

UPDATE嗨傢伙的感謝您的答覆的。上述問題已被GrecKo的解決方案解決,但即時通訊仍然不存在......我需要能夠使每個目標或目標設置在一個循環中。我認爲,通過讓上面的代碼工作,應該/將如果同樣的工作目標,當陣列中:

... 
property int i 
Component.onCompleted: { 

    var array = [rec1, rec2] 

    for(i = 0; i < array.length; i++){ 

     seq.access.targets = [array[i]] 
     seq.access.property = "y" 
     seq.access.to = 50 
     seq.start() 

    } 


} 

Rectangle{ 
    id: rec1 
    width: 50 
    height: 50 
    color: "red" 
} 

Rectangle{ 
    id: rec2 
    x: 100 
    width: 50 
    height: 50 
    color: "blue" 
} 
SequentialAnimation{ 
    id: seq 
    property alias access: num 
    NumberAnimation{id: num} 

} 

但它不工作。這是我需要做的。謝謝

回答

2

如果要在多個對象上應用相同的動畫,可以使用targets property爲動畫指定多個目標。

在你的榜樣,你會使用這樣的:

Component.onCompleted: { 
    seq.access.targets = [rec1, rec2] 
    seq.access.property = "y" 
    seq.access.to = 50 
    seq.start() 
} 
+0

'@GrecKo' 謝謝,那是偉大的,但我真的需要它要做的是在循環調用中工作。像'Component.onCompleted:{var array = [rec1,rec2] for(i = 0; i <2; i ++){seq.access.targets = [array [i]] seq.access.property =「y」 seq.access.to = 50 seq.start()}}' – LeeH

+0

你爲什麼要這樣? 你想延遲啓動它們嗎? – GrecKo

+0

嗨,不是延遲,而是來自任何位置的命令 – LeeH

0

請看看這段代碼。也許這就是你要找的。這個複製到您的main.qml

import QtQuick 2.5 
import QtQuick.Window 2.2 

Window { 
    visible: true 
    width: 1066 
    height: 600 

    property double time: 0 

    Rectangle { 
     width: 100 
     height: 100 
     x: 20 
     y: 50 + time * 250 
     color: "red" 
    } 
    Rectangle { 
     width: 100 
     height: 100 
     x: 220 
     y: 150 
     rotation: time * 360 
     color: "blue" 
    } 
    Rectangle { 
     width: 100 
     height: 100 
     x: 420 
     y: 50 + Math.pow(time, 1/3) * 250 
     color: "green" 
    } 
    Rectangle { 
     width: 100 
     height: 100 
     x: 620 
     y: 150 
     scale: 1 + Math.pow(time, 3) 
     color: "lightblue" 
    } 



    NumberAnimation on time { 
     running: true 
     loops: Animation.Infinite 
     from: 0 
     to: 1 
     duration: 1000 
    } 
} 

還什麼可以在你提供的示例工作是:

Component.onCompleted: { 

    seq.access.target = rec1 
    seq.access.property = "y" 
    seq.access.to = 50 
    seq.start() 

} 

Rectangle{ 
    id: rec1 
    width: 50 
    height: 50 
    color: "red" 
} 
Rectangle{ 
    id: rec2 
    x: 100 
    y: rec1.y 
    width: 50 
    height: 50 
    color: "blue" 
} 

SequentialAnimation{ 
    id: seq 
    property alias access: num 
    NumberAnimation{id: num} 
} 
+0

謝謝你 – LeeH