我原来的答复是不正确的:这里有一个更新的版本。
事件对象中的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();
感谢您的回答。它确实给顶部和左侧,但不是现有的。它给出了我最初设定的价值。请指导 – Volatil3 2011-03-15 14:58:39
啊,好的。你正在拖动图像? – Craig 2011-03-15 17:28:40
是的,我正在拖动它 – Volatil3 2011-03-15 17:43:17