2011-03-14 57 views
1

我在'touchend'事件中找到當前的X和Y位置。現在說ImageView的原始位置是; left = 100 and top = 100。在touchend它只是給出值爲2或8,10或9,11等數字現在我不知道什麼是圖像視圖的偏移量。如果它是lastLeft + x和lastTop + y,那麼它根本不會工作。我想要在Db的TouchEnd中保存圖像的當前位置,以便稍後恢復。Appcelerator:查找圖片的當前位置

請幫我

回答

1

我原來的答覆是不正確的:這裏有一個更新的版本。

事件對象中的globalpoint屬性包含用戶單擊的屏幕上的座標,而x和y屬性是用戶單擊的視圖中的座標。通過從另一箇中取出一個(並根據您的屏幕是否具有狀態欄,也允許這樣做),您可以在動畫完成後計算出對象的頂部/左側。

我已經把在1.6.0工作這表明該演示(只需創建一個新的項目,並與下面的代碼替換app.js)

// Windows 
var window = Ti.UI.createWindow(); 

var image = Ti.UI.createImageView({ 
    url: 'KS_nav_ui.png', 
    width: 100, 
    height: 100, 
    top: 0, 
    left: 0, 
    backgroundColor: '#f00' 
}); 

image.addEventListener('touchstart', function (e) { 

    var left = e.globalPoint.x - e.x, 
     top = e.globalPoint.y - e.y - 20; // The 20 accounts for status bar 

    Ti.API.info('Starting left: ' + left); 
    Ti.API.info('Starting top: ' + top); 

}); 

image.addEventListener('touchmove', function(e) { 

    var newX = e.x + image.animatedCenter.x - image.width/2; 
    var newY = e.y + image.animatedCenter.y - image.height/2; 

    image.animate({center:{x:newX,y:newY}, duration:1}); 

}); 

image.addEventListener('touchend', function (e) { 

    var left = e.globalPoint.x - e.x, 
     top = e.globalPoint.y - e.y - 20; // The 20 accounts for status bar 

    Ti.API.info('Finishing left: ' + left); 
    Ti.API.info('Finishing top: ' + top); 

}); 

window.add(image); 

window.open(); 
+0

感謝您的回答。它確實給頂部和左側,但不是現有的。它給出了我最初設定的價值。請指導 – Volatil3 2011-03-15 14:58:39

+0

啊,好的。你正在拖動圖像? – Craig 2011-03-15 17:28:40

+0

是的,我正在拖動它 – Volatil3 2011-03-15 17:43:17