我在“ touchend ”事件中找到当前的 X 和 Y 位置。现在说 ImageView 的原始位置是 ; left=100和top=100。在touchend上,它只给出 2 位数字的值,即 8,10 或 9,11 等。现在我不确定图像视图的偏移量是多少。如果它是 lastLeft+x 和 lastTop+y 那么它根本不起作用。我想在 Db 中的 TouchEnd 保存 Image 的当前位置,以便以后可以恢复它。
请帮助我
我在“ touchend ”事件中找到当前的 X 和 Y 位置。现在说 ImageView 的原始位置是 ; left=100和top=100。在touchend上,它只给出 2 位数字的值,即 8,10 或 9,11 等。现在我不确定图像视图的偏移量是多少。如果它是 lastLeft+x 和 lastTop+y 那么它根本不起作用。我想在 Db 中的 TouchEnd 保存 Image 的当前位置,以便以后可以恢复它。
请帮助我
我原来的答案是不正确的:这是一个更新的版本。
事件对象中的 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();