How to detect a 'pinch out' in a list of containers?

独自空忆成欢 提交于 2019-12-08 03:33:07

问题


I want to be able to pinch two containers in a list of containers away from each other to insert a new empty container between them. Similar to how the iPhone app “Clear” inserts new tasks (see for example the very first picture on this page https://www.raywenderlich.com/22174/how-to-make-a-gesture-driven-to-do-list-app-part-33 - the small red container is inserted when the two sorounding containers are pinched away from each other). Any hints on how I can achieve this in Codename One?


回答1:


Normally you would override the pinch method to implement pinch to zoom or similar calls. However, this won't work in this case as the pinch will exceed component boundaries and it wouldn't work.

The only way I can think of doing this is to override the pointerDragged(int[],int[]) method in Form and detect the pinch motion as growing to implement this. You can check out the code for pinch in Component.java as it should be a good base for this:

public void pointerDragged(int[] x, int[] y) {
    if (x.length > 1) {
        double currentDis = distance(x, y);

        // prevent division by 0
        if (pinchDistance <= 0) {
            pinchDistance = currentDis;
        }
        double scale = currentDis / pinchDistance;
        if (pinch((float)scale)) {
            return;
        }
    }
    pointerDragged(x[0], y[0]);
}
private double distance(int[] x, int[] y) {
    int disx = x[0] - x[1];
    int disy = y[0] - y[1];
    return Math.sqrt(disx * disx + disy * disy);
}

Adding the entry is simple, just place a blank component in the place and grow its preferred size until it reaches the desired size.



来源:https://stackoverflow.com/questions/39673861/how-to-detect-a-pinch-out-in-a-list-of-containers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!