SGGestureRecognizer is a graphics gesture recognizer based on Dollar One Alghrithm for iOS. It's more powerful than the UIGestureRecognizeer, it can record any gesture graphic at one stroke, suck as a star, an triangle and so on. You can use these graphics to execute specific action.
The direction of the gesture is import, a closewise gesture is not equal to an anticlockwise gesture.
- Download SGGestureRecognizer and try out the inclued iPhone example app.
Drag the SGGestureRecognizer
folder to your project.
#import "SGGestureRecognizer.h"
Firstly, you should sample some points on the gesture path and put them to an NSArray
, such as NSMutableArray<NSValue *> *samplePoints
, for recognize, you should name the gesture when you create a gesture set.
SGDollarOneManager *mgr = [SGDollarOneManager sharedManager];
SGGestureSet *set = [SGGestureSet gestureSetWithName:<name of the gesture> points:self.samplePoints];
if (set.countPoints) {
[mgr addGestureSet:set];
}
When you add a gesture set, the manager will standardize the set and generate vector for this set, then save it to the save path on disk.
You also should sample some points on the gesture path and pass to SGGestureSet
, the recognizer will standardize the set and generate vector to compare with the vectors in gesture library.
SGDollarOneManager *mgr = [SGDollarOneManager sharedManager];
SGGestureSet *set = [SGGestureSet gestureSetWithPoints:self.samplePoints];
NSString *gesName = [mgr recognizeGestureSet:set];
if the gesName is not nil, that means the gesture is recognize succeeded.
You can preview and delete gestures in library by present an SGGesturePreviewController
embedded in an UINavigationController
.
- (void)preview {
SGGesturePreviewController *vc = [[SGGesturePreviewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:nil];
}
The SGGesturePreviewController
will load gestures from the SGGestureManager
, you can delete a gesture by slide left a gesture cell.
You can customize the gesture library save and load path by change the property in SGGestureManager
.
The coed below change the load path to main bundle, when you change the load path, the manager will reload gestures from the new path.
[SGDollarOneManager sharedManager].libLoadPath = [[NSBundle mainBundle] pathForResource:@"gestureLib.gs" ofType:nil];
The save path is the Caches Directory by default.
There are three params in the manager property, they are samplePointCount
, threshold
and gestureSize
.
When the manager resample the set, there will be some uniform distributed points on the gesture curve, the number of the points is defined in samplePointCount
.
When the manager recognize a gesture, it use Cosine Similarity
to compare the vector of the gesture to be recognized and the vectors in the gesture library, if there cosine similarity is less than the threshold, it will be considered, when all vectors are enumerated, the manager will choose the best result to return.
When the manager standardize the set, the gesture curve will be scale to standard size, the standard size is the gestureSize defined in the manager.