【问题标题】:Appcelerator:Finding current position of ImageAppcelerator:查找图像的当前位置
【发布时间】:2011-03-14 19:00:10
【问题描述】:

我在 'touchend' 事件中找到当前 X 和 Y 位置。现在说 ImageView 的原始位置是 ; left=100top=100。在 touchend 上,它只给出 2 位数字的值,即 8,10 或 9,11 等。现在我不确定图像视图的偏移量是多少。如果它是 lastLeft+x 和 lastTop+y 那么它根本不起作用。我想在 Db 中的 TouchEnd 保存 Image 的当前位置,以便以后恢复它。

请帮帮我

【问题讨论】:

    标签: iphone imageview appcelerator appcelerator-mobile


    【解决方案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();
    

    【讨论】:

    • 感谢您的回答。它确实给出了顶部和左侧,但不是当前的。它给出了我最初设置的值。请指导
    • 啊,好的。你在拖动图像?
    • 您可能需要在事件上使用“globalPoint”属性,因为它会根据屏幕给出事件的坐标。它不会完全映射到顶部/左侧,但如果您知道图像的宽度/原始位置,您应该能够从那里计算出来。我看看能不能举个例子..
    • 如果你能做到,那将是一个很大的帮助。我正在使用 2 个 ImageView,一个 imageview 被拖到另一个上。顺便说一句,globalpoint 不返回与 event.x 和 y 相同的值吗?
    • 我已经用一个演示更新了我的答案,我认为它可以满足您的需求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多