Fix Y-coord on touches

Summary:
public

D2670028 updated the x/y positions of touch events to be relative to the window, but measure still uses the location on the screen. Therefore, in Touchable.js, we were seeing taps get inproperly invalidated because they were erroneously considered outside of the touch rect. This diff moves back to the old version of pageX/Y on touch events that's compatible with the current version of measure.

Reviewed By: nicklockwood

Differential Revision: D2724917

fb-gh-sync-id: 978ae26fcaa23c47a4f619e2b7ff2d078388ae95
This commit is contained in:
Dave Miller 2015-12-04 20:42:19 -08:00 committed by facebook-github-bot-0
parent 383d991c40
commit fa884ee5e6
1 changed files with 14 additions and 24 deletions

View File

@ -35,35 +35,25 @@ import com.facebook.react.uimanager.PixelUtil;
* given {@param event} instance. This method use {@param reactTarget} parameter to set as a
* target view id associated with current gesture.
*/
private static WritableArray createsPointersArray(int reactTarget, TouchEvent event) {
private static WritableArray createsPointersArray(int reactTarget, TouchEvent touchEvent) {
MotionEvent event = touchEvent.getMotionEvent();
WritableArray touches = Arguments.createArray();
MotionEvent motionEvent = event.getMotionEvent();
// Calculate the coordinates for the target view.
// The MotionEvent contains the X,Y of the touch in the coordinate space of the root view
// The TouchEvent contains the X,Y of the touch in the coordinate space of the target view
// Subtracting them allows us to get the coordinates of the target view's top left corner
// We then use this when computing the view specific touches below
// Since only one view is actually handling even multiple touches, the values are all relative
// to this one target view.
float targetViewCoordinateX = motionEvent.getX() - event.getViewX();
float targetViewCoordinateY = motionEvent.getY() - event.getViewY();
// Calculate raw-to-relative offset as getRawX() and getRawY() can only return values for the
// pointer at index 0. We use those value to calculate "raw" coordinates for other pointers
float offsetX = event.getRawX() - event.getX();
float offsetY = event.getRawY() - event.getY();
for (int index = 0; index < motionEvent.getPointerCount(); index++) {
for (int index = 0; index < event.getPointerCount(); index++) {
WritableMap touch = Arguments.createMap();
// pageX,Y values are relative to the RootReactView
// the motionEvent already contains coordinates in that view
touch.putDouble(PAGE_X_KEY, PixelUtil.toDIPFromPixel(motionEvent.getX(index)));
touch.putDouble(PAGE_Y_KEY, PixelUtil.toDIPFromPixel(motionEvent.getY(index)));
// locationX,Y values are relative to the target view
// To compute the values for the view, we subtract that views location from the event X,Y
float locationX = motionEvent.getX(index) - targetViewCoordinateX;
float locationY = motionEvent.getY(index) - targetViewCoordinateY;
touch.putDouble(LOCATION_X_KEY, PixelUtil.toDIPFromPixel(locationX));
touch.putDouble(LOCATION_Y_KEY, PixelUtil.toDIPFromPixel(locationY));
touch.putDouble(PAGE_X_KEY, PixelUtil.toDIPFromPixel(event.getX(index) + offsetX));
touch.putDouble(PAGE_Y_KEY, PixelUtil.toDIPFromPixel(event.getY(index) + offsetY));
touch.putDouble(LOCATION_X_KEY, PixelUtil.toDIPFromPixel(event.getX(index)));
touch.putDouble(LOCATION_Y_KEY, PixelUtil.toDIPFromPixel(event.getY(index)));
touch.putInt(TARGET_KEY, reactTarget);
touch.putDouble(TIMESTAMP_KEY, motionEvent.getEventTime());
touch.putDouble(POINTER_IDENTIFIER_KEY, motionEvent.getPointerId(index));
touch.putDouble(TIMESTAMP_KEY, event.getEventTime());
touch.putDouble(POINTER_IDENTIFIER_KEY, event.getPointerId(index));
touches.pushMap(touch);
}