Skip to content

Commit

Permalink
EddyVerbruggen#6 Add Force Touch callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Dec 11, 2015
1 parent 2e19c0b commit c67ffa7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ so you can check at runtime if the user's device is supported.
});
```

### watchForceTouches
You can get a notification when the user force touches the webview.
The plugin defines a Force Touch when at least 75% of the maximum force is applied to the screen.
Your app will receive the x and y coordinates, so you have to figure out which UI element was touched.

Useful for context menu's, zooming in on images, whatnot.

```js
ThreeDeeTouch.watchForceTouches(function(result) {
console.log("force touch % " + result.force);
console.log("force touch x coordinate " + result.x);
console.log("force touch y coordinate " + result.y);
});
```

### configureQuickActions
When your app starts you can add those fancy Quick Actions to the Home Screen icon.
You can configure up to four icons and they are 'cached' until you pass in a new set of icons.
Expand Down Expand Up @@ -193,6 +208,7 @@ This is the same as the `type` param of `configureQuickActions`, so it's what yo
`onHomeIconPressed` as `payload.type`. Just do something cool with that info.

## 6. Changelog
* 1.3.0 You can now receive notifications when the user applies a 'Force Touch' on the webview.
* 1.2.2 Documentation on how to localize the title and subtitle of your static icons.
* 1.2.1 Documentation on how to add static icons to your app.
* 1.2.0 iOS 9.1 added a lot of new iconTypes to choose from. Thanks #2!
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"keywords": [
"3DTouch",
"3D Touch",
"Force",
"Force Touch",
"ecosystem:cordova",
"cordova-ios"
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<plugin
id="cordova-plugin-3dtouch"
version="1.2.2"
version="1.3.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0">

<name>3D Touch</name>
Expand Down
2 changes: 1 addition & 1 deletion src/ios/app/AppDelegate+threedeetouch.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "AppDelegate.h"

@interface AppDelegate (applewatch)
@interface AppDelegate (threedeetouch)
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler;

@end
11 changes: 11 additions & 0 deletions src/ios/app/ThreeDeeTouch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@

- (void) isAvailable:(CDVInvokedUrlCommand*)command;

- (void) watchForceTouches:(CDVInvokedUrlCommand*)command;

- (void) configureQuickActions:(CDVInvokedUrlCommand*)command;

- (void) enableLinkPreview:(CDVInvokedUrlCommand*)command;
- (void) disableLinkPreview:(CDVInvokedUrlCommand*)command;

@end

@class ForceTouchRecognizer;
@interface ForceTouchRecognizer : UIGestureRecognizer {
ForceTouchRecognizer * ForceTouchRecognizer;
}
@property NSString* callbackId;
@property id<CDVCommandDelegate> commandDelegate;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
@end
42 changes: 42 additions & 0 deletions src/ios/app/ThreeDeeTouch.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ - (void) isAvailable:(CDVInvokedUrlCommand *)command {
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) watchForceTouches:(CDVInvokedUrlCommand*)command {
ForceTouchRecognizer* forceTouchRecognizer = [[ForceTouchRecognizer alloc] initWithTarget:self action:nil];
forceTouchRecognizer.callbackId = command.callbackId;
forceTouchRecognizer.commandDelegate = self.commandDelegate;
[self.webView addGestureRecognizer: forceTouchRecognizer];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
pluginResult.keepCallback = [NSNumber numberWithBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) enableLinkPreview:(CDVInvokedUrlCommand *)command {
self.webView.allowsLinkPreview = YES;
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
Expand Down Expand Up @@ -99,3 +110,34 @@ - (UIApplicationShortcutIconType) UIApplicationShortcutIconTypeFromString:(NSStr
}
}
@end


@implementation ForceTouchRecognizer

double lastEvent = 0;

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch* touch in touches) {
CGFloat percentage = (touch.force / touch.maximumPossibleForce) * 100;
if (percentage >= 75) {
// let's not flood the callback with multiple hits within the same second
NSTimeInterval ts = touch.timestamp;
int diff = ts - lastEvent;
if (diff > 0) {
lastEvent = ts;
CGPoint coordinates = [touch locationInView:self.view];
NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[NSString stringWithFormat:@"%d", (int)percentage] , @"force",
[NSString stringWithFormat:@"%d", (int)coordinates.x], @"x",
[NSString stringWithFormat:@"%d", (int)coordinates.y], @"y",
nil];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
pluginResult.keepCallback = [NSNumber numberWithBool:YES];
[_commandDelegate sendPluginResult:pluginResult callbackId:_callbackId];
}
}
}
}
@end

4 changes: 4 additions & 0 deletions www/ThreeDeeTouch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ ThreeDeeTouch.prototype.isAvailable = function (onSuccess) {
exec(onSuccess, null, "ThreeDeeTouch", "isAvailable", []);
};

ThreeDeeTouch.prototype.watchForceTouches = function (onSuccess) {
exec(onSuccess, null, "ThreeDeeTouch", "watchForceTouches", []);
};

ThreeDeeTouch.prototype.enableLinkPreview = function (onSuccess) {
exec(onSuccess, null, "ThreeDeeTouch", "enableLinkPreview", []);
};
Expand Down

0 comments on commit c67ffa7

Please sign in to comment.