Skip to content

Instantly share code, notes, and snippets.

@snown
Created February 18, 2014 17:29
Show Gist options
  • Save snown/9075560 to your computer and use it in GitHub Desktop.
Save snown/9075560 to your computer and use it in GitHub Desktop.
A UIGestureRecognizer Category to generate NSStrings for use with NSLog
#import <UIKit/UIKit.h>
@interface UIGestureRecognizer (NSStringFunctions)
/**
* Returns a string formatted to display the pertenent name of the
* `UIGestureRecognizerState`
*
* Often this just returns the text of the variable name after
* "UIGestureRecognizerState". For Example, `UIGestureRecognizerStatePossible`
* returns "Possible".
*
* @param state The `UIGestureRecognizerState` to format.
*
* @return A formatted `UIGestureRecognizerState` name string.
*/
UIKIT_EXTERN NSString *NSStringFromUIGestureRecognizerState(UIGestureRecognizerState state);
@end
#import "UIGestureRecognizer+NSStringFunctions.h"
@implementation UIGestureRecognizer (NSStringFunctions)
NSString *NSStringFromUIGestureRecognizerState(UIGestureRecognizerState state) {
/*
UIGestureRecognizerStatePossible, // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan, // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged, // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded, // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled, // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed, // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
// Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
*/
NSString *resultString;
switch (state) {
case UIGestureRecognizerStatePossible:
resultString = @"Possible";
break;
case UIGestureRecognizerStateBegan:
resultString = @"Began";
break;
case UIGestureRecognizerStateChanged:
resultString = @"Changed";
break;
case UIGestureRecognizerStateEnded:
resultString = @"Ended";
break;
case UIGestureRecognizerStateCancelled:
resultString = @"Cancelled";
break;
case UIGestureRecognizerStateFailed:
resultString = @"Failed";
break;
default:
break;
}
return resultString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment