Reputation: 24426
Is there a way to give UIGestureRecognizer a shape (a series of co ords) which it can use to trigger an action when the user draws the shape with his fingers? I'm thinking of letter shapes, but it could be anything.
EDIT:
I found this https://github.com/chrismiles/CMUnistrokeGestureRecognizer which will probably do what I want.
Upvotes: 0
Views: 967
Reputation: 4628
Unfortunately implementing custom gesture recognisers isn't as simple as providing a UIGestureRecognizer
with a shape or series of points. You have to subclass UIGestureRecognizer
and write code that tracks the user's interaction through touchesBegan:withEvent:
and touchesMoved:withEvent:
etc. Then, based on line lengths and angles etc. of the gesture the user draws you determine whether it successfully matched what you were expecting and fire the UIGestureRecognizer
callback.
This results in inherent complications as users are not very precise when squiggling gestures with their fingers. You would have to design your gestures with a tolerance as to what was recognised; too strict and it will be useless, too generic and it will report too many false positives.
I suspect that if you were attempting to recognise a large quantity of gestures, like the letters of the alphabet for instance, instead of implementing 26 different gesture recognisers you would be better off writing a generic one that recorded the user's input once and checked whether it matched a selection of gesture definitions you have stored somewhere. Then implement a custom callback that tells the handler which gesture it matched.
The very reputable 'Beginning iOS Development: Exploring the iOS SDK' series from Apress dedicate a small portion of a chapter to implementing a custom gesture recogniser. The accompanying source code can be downloaded from the official Apress website here (Source Code/Downloads tab at the bottom).
See pages 627-632 in chapter 17: 'Taps, Touches, and Gestures'.
The Gesture Recognizers chapter of Apple's Event Handling Guide for iOS contains a 'Creating a Custom Gesture Recognizer' section that also has relevant information and examples.
Upvotes: 2