Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-25839] TiAPI: Touch event coordinate units do not match between platforms

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2019-01-11T02:28:58.000+0000
Affected Version/sRelease 6.0.0
Fix Version/sRelease 8.0.0
ComponentsAndroid, iOS
Labelsandroid, breaking-change, defaultunit, ios, parity, touch
ReporterJoshua Quick
AssigneeJoshua Quick
Created2018-03-06T03:44:25.000+0000
Updated2019-01-11T02:29:03.000+0000

Description

*Summary:* The (x,y) units provided by View event's "touchstart", "touchmove", "touchend", and "touchcancel" do not match between Android and iOS. Both platforms ignore the "ti.ui.defaultunit" property setting in "tiapp.xml". On Android, the touch coordinates are always in "px" (ie: pixels). On iOS, the touch coordinates are always in "dip" (aka: "dp"). *Steps to Reproduce:*

Build and run the below code on Android.

Drag your finger in the gray view.

Notice that the blue square does *not* correctly following your finger.

Build and run on iOS.

Drag your finger in the gray view.

Notice that the blue square *correctly* follows your finger.

Change "tiapp.xml" property "ti.ui.defaultunit" to "px".

Build and run on iOS.

Drag your finger in the gray view.

Notice that the blue square does *not* correctly following your finger.

Build and run on Android.

Drag your finger in the gray view.

Notice that the blue square *correctly* follows your finger.

function onTouch(event) {
	if (event.source == containerView) {
		touchView.center = { x: event.x, y: event.y };
	}
}

var window = Ti.UI.createWindow(
{
	layout: "vertical",
	fullscreen: true,
	theme: "Theme.AppCompat.NoTitleBar",
});
window.add(Ti.UI.createLabel(
{
	text: "Touch Drag Test",
	top: "20dp",
}));
var containerView = Ti.UI.createView(
{
	touchEnabled: true,
	backgroundColor: "gray",
	top: "10dp",
	bottom: "20dp",
	width: "90%",
});
var touchView = Ti.UI.createView(
{
	touchEnabled: false,
	backgroundColor: "#008800",
	width: "100dp",
	height: "100dp",
});
containerView.add(touchView);
containerView.addEventListener("touchstart", onTouch);
containerView.addEventListener("touchmove", onTouch);
containerView.addEventListener("touchend", onTouch);
containerView.addEventListener("touchcancel", onTouch);
containerView.addEventListener("click", onTouch);
containerView.addEventListener("dblclick", onTouch);
containerView.addEventListener("doubletap", onTouch);
window.add(containerView);
window.open();
*Work-Around:* The following will work-around this issue on both Android and iOS...
// Fetch the default unit property set in "tiapp.xml".
var defaultUnit = Ti.App.Properties.getString("ti.ui.defaultunit", "dip");
if (defaultUnit === "dp") {
	defaultUnit = "dip";
} else if (defaultUnit === "system") {
	defaultUnit = (Ti.Platform.name === "android") ? "px" : "dip";
}

// Do the below when a touch event has been received.
function onTouch(event) {
	if (Ti.Platform.name === "android") {
		event.x = Ti.UI.convertUnits(event.x + "px", defaultUnit);
		event.y = Ti.UI.convertUnits(event.y + "px", defaultUnit);
	} else if ((Ti.Platform.name === "iOS") || (Ti.Platform.name === "iPhone OS")) {
		event.x = Ti.UI.convertUnits(event.x + "dip", defaultUnit);
		event.y = Ti.UI.convertUnits(event.y + "dip", defaultUnit);
	}
	touchView.center = { x: event.x, y: event.y };
}
*Ideal Solution:* Convert native touch coordinates to use "ti.ui.defaultunit" on both Android and iOS. This way views can easily be dragged since their "x", "y", and "center" properties already respect the "ti.ui.defaultunit" property. Note that this would be a breaking change.

Comments

  1. Andrea Vitale 2018-05-04

    Hi have a similar issue using
    Ti.UI.create2DMatrix().translate(0, 100)
    On Android the value 100 is evaluated as 100px, on iOS it is correctly evaluated as 100dp.
  2. Joshua Quick 2018-11-01

    PR (master): https://github.com/appcelerator/titanium_mobile/pull/10414
  3. Joshua Quick 2018-11-15

    [~Andrea.Vitale], I've written up a separate ticket for the 2DMatrix.translate() coordinate issue. Please see: [TIMOB-26563] Thanks for bringing this issue to our attention.
  4. Samir Mohammed 2018-11-30

    *Closing ticket.* Fix verified in SDK Version 8.0.0.v20181129161342. Test and other information can be found at: https://github.com/appcelerator/titanium_mobile/pull/10414

JSON Source