Skip to content

Instantly share code, notes, and snippets.

@uupaa
Created September 22, 2016 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uupaa/4038e48722ea34898e5f6d5822865b2e to your computer and use it in GitHub Desktop.
Save uupaa/4038e48722ea34898e5f6d5822865b2e to your computer and use it in GitHub Desktop.
iPhone/iPad Pro 3D touch deep detection.
function _deepDetection(callback) {
var that = this;
var device = this._DEVICE;
var osVer = parseFloat(this._OS_VERSION);
if (this._iOS) {
_has3DTouchDevice_iOS(function(has) {
switch (device) {
case "iPhone 7 Plus":
case "iPhone 7":
case "iPhone 6s Plus":
case "iPhone 6s":
if (osVer >= 9.0) {
that._3D_TOUCH = true;
}
break;
case "iPad Pro 9.7":
case "iPad Pro":
if (osVer >= 10.0) {
that._3D_TOUCH = true;
}
break;
}
callback(that);
});
} else {
callback(that);
}
}
function _has3DTouchDevice_iOS(callback) { // @arg Function - callback(has3DTouchFunction:Boolean):void
var customTouchEventType = "__taptaptap__";
var customTouchEvent = _createCustomTouchEvent(customTouchEventType);
if (customTouchEvent) {
document.body.addEventListener(customTouchEventType, _handleEvent, false);
document.body.dispatchEvent(customTouchEvent);
} else {
callback(false);
}
function _handleEvent(event) {
var has3DTouchFunction = false;
if (event.touches && event.touches[0] && "force" in event.touches[0]) {
console.warn("force", event.touches[0].force);
has3DTouchFunction = true;
}
document.body.removeEventListener(customTouchEventType, _handleEvent, false);
callback(has3DTouchFunction);
}
function _createCustomTouchEvent(eventType) {
if (!Document.prototype.createTouch) {
return null;
}
var view = window;
var target = document.body;
var identifier = 100;
var pageX = 100;
var pageY = 100;
var screenX = 100;
var screenY = 100;
var clientX = 100;
var clientY = 100;
var scale = 1.0;
var rotation = 0.0;
var canBubble = true;
var cancelable = true;
var detail = 100;
var ctrlKey = false;
var altKey = false;
var shiftKey = false;
var metaKey = false;
// createTouch spec: https://developer.apple.com/reference/webkitjs/1777836-createtouch
var touch = document.createTouch(view, target, identifier, pageX, pageY, screenX, screenY);
var touches = document.createTouchList(touch);
var customTouchEvent = document.createEvent("TouchEvent");
var targetTouches = touches;
var changedTouches = touches;
// initTouchEvent spec: https://developer.apple.com/reference/webkitjs/touchevent/1631943-inittouchevent
customTouchEvent.initTouchEvent(
eventType, canBubble, cancelable, view, detail,
screenX, screenY, clientX, clientY,
ctrlKey, altKey, shiftKey, metaKey,
touches, targetTouches, changedTouches,
scale, rotation);
return customTouchEvent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment