你好,我正試圖在NativeScript視圖上實現一個移動的背景圖像。Nativescript無限翻譯動畫
佈局看起來像這樣
login.xml
<Page loaded="loaded" android:actionBarHidden="true">
<GridLayout>
<Image src="~/img/haloose_bg.png" id="bg"/>
<StackLayout orientation="vertical" verticalAlignment="center" id="sl_login">
...
</StackLayout>
</GridLayout>
</Page>
我想Image
以沿隨機方向運動的背景
我曾嘗試以下方法:
1)設置間隔方法
utils.js
utils.animateBG = function(container,id,duration){
var newx = newy = Math.random() + 1.2;
container.getViewById(id).animate({
translate : {x: newx - 10 , y : newy + 70 },
duration : duration
});
}
login.js
exports.loaded = function(args){
page = args.object;
setInterval(utils.animateBG(page,"bg",3000),3000);
}
我然後清除的時間間隔時用戶點擊一按鈕或離開視圖。這種方法使應用程序在4秒後崩潰。
2)雖然環方法
login.js
while(!user.hasClickedSomething){
utils.animateBG(page,"bg",3000);
}
這種方法使得白色屏幕上的應用程序凍結。
3)遞歸方法
在這裏,我編輯的動畫的方法:
utils.js
utils.animateBG = function(container,id,duration,continueAnimation){
if(continueAnimation){
var newx = newy = Math.random() + 1.2;
container.getViewById(id).animate({
scale : { x: newx, y: newy},
translate : {x: newx - 10 , y : newy + 70 },
duration : duration
}).then(function(){
utils.animateBG(container,id,duration,continueAnimation);
});
}
}
然後我打電話它並傳遞user.continueAnimation
作爲條件應該停止循環。 user
是一個可觀察的視圖模型,默認情況下該頁面的continueAnimation
字段設置爲true
。
login.js
exports.pageloaded = function(args){
page=args.object;
page.bindingContext = user;
utils.animateBG(page,"bg",3000,user.continueAnimation);
}
然後我嘗試設置user.continueAnimation
到false
當我點擊其他按鈕,但不知何故,它總是在方法內保持真實。這導致動畫永不停止,如果我轉到另一個視圖並返回,該應用程序會凍結或崩潰。
有沒有人實施我想要做的?有沒有更好的方法來做到這一點? 謝謝
這種方法看上去很不錯!代碼不起作用雖然=(我會通過文檔,看看有什麼需要修復! –
它爲我工作 - 嘗試安裝最新的NativeScript 2.0.0 –
現在它!謝謝 –