【问题标题】:Scroll up and down by drag the table通过拖动表格上下滚动
【发布时间】:2018-07-05 11:41:00
【问题描述】:
 table1.addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
           scroll=y;
            System.out.print(scroll);
            return true;
        }

        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {

            table1.setPosition((scroll+(y)));
        }});

我想通过拖动表格来上下滚动表格(例如手机上的 Facebook)我无法在 Y 上做这个数学运算我不知道为什么当我拖动它时它会发疯。

【问题讨论】:

    标签: java scroll libgdx drag


    【解决方案1】:

    InputListener方法中的参数x和y是局部坐标(即相对于你的table),所以需要将它们转换为table的父坐标系:

    table.addListener(new InputListener() {
    
        Vector2 vec = new Vector2();
    
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            table.localToParentCoordinates(vec.set(x, y));
            return true;
        }
    
        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {
            float oldTouchY = vec.y;
            table.localToParentCoordinates(vec.set(x, y));
            table.moveBy(0f, vec.y - oldTouchY);
        }
    });
    

    顺便说一句,在您的情况下,使用ScrollPane 可能会更好、更方便:https://github.com/libgdx/libgdx/wiki/Scene2d.ui#scrollpane

    更新:

    实际上,由于您只需要deltaY,因此无需转换坐标即可:

    table.addListener(new InputListener() {
    
        float touchY;
    
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            touchY = y;
            return true;
        }
    
        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {
            // since you move your table along with pointer, your touchY will be the same in table's local coordinates
            float deltaY = y - touchY;
            table.moveBy(0f, deltaY);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      相关资源
      最近更新 更多