基于PennyPincher算法的快速手势识别:PennyPincher
Demo
Requirements
>= iOS 8, Xcode 7, Swift 2.0
Installation
Recommended installation options are via Carthage or manual installation.
Carthage:
PennyPincher supports installation via Carthage:
- Add the following line to your Cartfile:github "fe9lix/PennyPincher" >= 1.0
- Runcarthage update
Manual:
- Drag the folderPennyPincherExample/Carthage/Build/iOS/PennyPincher.frameworkinto your Xcode project and select "Copy items if needed".
- Make sure that the framework is added underEmbedded Binariesin the general section of your project's target settings.
Usage
Please see theViewControllerof the example project on how to use PennyPincher. Although you can use thePennyPincherclass directly, the easiest way is to instantiate its gesture recognizer class, configure it, and add it to a view:
let pennyPincherGestureRecognizer = PennyPincherGestureRecognizer() pennyPincherGestureRecognizer.enableMultipleStrokes = true pennyPincherGestureRecognizer.allowedTimeBetweenMultipleStrokes = 0.2 pennyPincherGestureRecognizer.cancelsTouchesInView = false pennyPincherGestureRecognizer.addTarget(self, action: "didRecognize:") view.addGestureRecognizer(pennyPincherGestureRecognizer)
In the code above, the following properties are set:
- enableMultipleStrokes: Allows gestures to be composed of multiple separate strokes, as long as the pause between strokes does not exceedallowedTimeBetweenMultipleStrokes. When the property is set tofalse, the gesture recognizer transitions to the cancelled state as soon as the user lifts the finger.
- allowedTimeBetweenMultipleStrokes: See above.
- cancelsTouchesInView: Regular iOS gesture recongizer property. Might be set tofalsewhen you want to ensure that touches are still delivered to the attached view.
The target-action pair is executed for state changes triggered by the recognizer. You can use thestateproperty to react accordingly in the UI. Theresultproperty returns a tuple consisting of the recognizedPennyPincherTemplateand CGFloat value indicating the similarity. For example:
guard let (template, similarity) = pennyPincherGestureRecognizer.result else { print("Could not recognize.") return } let similarityString = String(format: "%.2f", similarity) print("Template: \(template.id), Similarity: \(similarityString)")
You can add and remove templates by modifying thetemplatesarray property of the recognizer. ThePennyPincherclass provides a static method to create new templates of typePennyPincherTemplate(a struct). Required parameters are theid(a unique string) andpoints(an array of CGPoints).
For example:
let template = PennyPincher.createTemplate("templateID", points: points) pennyPincherGestureRecognizer.templates.append(template)
Templates could be serialized and saved to disk and then loaded again into memory when the application launches. PennyPincher works pretty well with only one template per gesture (id) but, depending on your use case, you can increase its accuracy by adding more for each gesture.