2011-03-14 58 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