@Override public void onBrowserEvent(Event event) { switch (event.getTypeInt()) { case Event.ONTOUCHSTART: startPoint = new Point(event.getChangedTouches().get(0).getClientX(), event.getChangedTouches().get(0).getClientY()); break; case Event.ONTOUCHMOVE: endPoint = new Point(event.getChangedTouches().get(0).getClientX(), event.getChangedTouches().get(0).getClientY()); double dx = startPoint.getX() - endPoint.getX(); double dy = startPoint.getY() - endPoint.getY(); if (Math.abs(dx) > SWIPE_X_MIN) { if (Math.abs(dy) < SWIPE_Y_MAX) { // Decided it really is a horizontal swipe event.preventDefault(); if (dx < 0) { Window.alert("swiped right"); } else { Window.alert("swiped left"); } } } break; } }
private boolean isSignificantMove(Event event) { if (touchStart == null) { // no touch start return false; } /* * TODO calculate based on real distance instead of separate * axis checks */ Touch touch = event.getChangedTouches().get(0); if (Math.abs(touch.getClientX() - touchStartX) > TouchScrollDelegate.SIGNIFICANT_MOVE_THRESHOLD) { return true; } if (Math.abs(touch.getClientY() - touchStartY) > TouchScrollDelegate.SIGNIFICANT_MOVE_THRESHOLD) { return true; } return false; }
/** * Calculates how many pixels away the user's finger has traveled. This * reduces the chance of small non-intentional movements from canceling * the long press detection. * * @param event * the Event for which to check the move distance * @return true if this is considered an intentional move by the user */ protected boolean isSignificantMove(Event event) { if (touchStart == null) { // no touch start return false; } // Calculate the distance between touch start and the current touch // position Touch touch = event.getChangedTouches().get(0); int deltaX = touch.getClientX() - touchStartX; int deltaY = touch.getClientY() - touchStartY; int delta = deltaX * deltaX + deltaY * deltaY; // Compare to the square of the significant move threshold to remove // the need for a square root if (delta > TouchScrollDelegate.SIGNIFICANT_MOVE_THRESHOLD * TouchScrollDelegate.SIGNIFICANT_MOVE_THRESHOLD) { return true; } return false; } }
Touch touch = event.getChangedTouches().get(0); touchStartX = touch.getClientX(); touchStartY = touch.getClientY();
touchStart = event; isDragging = false; Touch touch = event.getChangedTouches().get(0);
case Event.ONTOUCHSTART: touchStart = event; Touch touch = event.getChangedTouches().get(0);