Skip to content

Commit

Permalink
Merge pull request #4 from mlynch/master
Browse files Browse the repository at this point in the history
feat(taptic): gesture control for selection events.
  • Loading branch information
EddyVerbruggen authored Oct 6, 2016
2 parents bea7e0e + 48c0db1 commit d1b3377
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ios/TapticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
- (void) strongBoom:(CDVInvokedUrlCommand*)command;
- (void) burst:(CDVInvokedUrlCommand*)command;

// This property stores a selection feedback generator during
// gestures, as per https://developer.apple.com/reference/uikit/uifeedbackgenerator#2555399
@property UISelectionFeedbackGenerator *selectionGenerator;

@end
52 changes: 52 additions & 0 deletions src/ios/TapticEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ - (void) notification:(CDVInvokedUrlCommand *)command
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
}

/**
* Trigger selection feedback in one burst, useful for non-gesture based
* selection feedback.
*/
- (void) selection:(CDVInvokedUrlCommand *)command
{
UISelectionFeedbackGenerator *generator = [UISelectionFeedbackGenerator new];
Expand All @@ -52,6 +56,54 @@ - (void) selection:(CDVInvokedUrlCommand *)command
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
}


/**
* Used to prepare a taptic event for future taptic events in a gesture.
*/
- (void) gestureSelectionStart:(CDVInvokedUrlCommand *)command
{
if(!self.selectionGenerator) {
self.selectionGenerator = [UISelectionFeedbackGenerator new];
if (self.selectionGenerator == nil || isSimulator) {
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unsupported Operating System"] callbackId:command.callbackId];
return;
}
}

[self.selectionGenerator prepare];


[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
}

/**
* While in a gesture, we can efficiently trigger a selection taptic event.
* gestureSelectionStart should be called first.
*/
- (void) gestureSelectionChanged:(CDVInvokedUrlCommand *)command
{
if(!self.selectionGenerator) {
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unsupported Operating System"] callbackId:command.callbackId];
return;
}

[self.selectionGenerator selectionChanged];
[self.selectionGenerator prepare];

[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
}

/**
* Called at the end of a gesture to clean up the taptic reference.
*/
- (void) gestureSelectionEnd:(CDVInvokedUrlCommand *)command
{
// Free up the selection generator
self.selectionGenerator = nil;

[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
}

- (void) impact:(CDVInvokedUrlCommand *)command
{
NSDictionary* options = command.arguments[0];
Expand Down
9 changes: 9 additions & 0 deletions www/TapticEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ TapticEngine.prototype.impact = function (options, onSuccess, onFail) {
exec(onSuccess, onFail, "TapticEngine", "impact", [options]);
};

TapticEngine.prototype.gestureSelectionStart = function (onSuccess, onFail) {
exec(onSuccess, onFail, "TapticEngine", "gestureSelectionStart", []);
};
TapticEngine.prototype.gestureSelectionChanged = function (onSuccess, onFail) {
exec(onSuccess, onFail, "TapticEngine", "gestureSelectionChanged", []);
};
TapticEngine.prototype.gestureSelectionEnd = function (onSuccess, onFail) {
exec(onSuccess, onFail, "TapticEngine", "gestureSelectionEnd", []);
};

module.exports = new TapticEngine();

0 comments on commit d1b3377

Please sign in to comment.