通过拖动 table 上下滚动
Scroll up and down by drag the table
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)));
}});
我想通过拖动 table 上下滚动 table(就像 phone 上的 Facebook)我无法在 Y
不知道为什么一拖就疯了
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);
}
});
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)));
}});
我想通过拖动 table 上下滚动 table(就像 phone 上的 Facebook)我无法在 Y
不知道为什么一拖就疯了
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);
}
});