Permalink
Browse files

Merge pull request #2 from subdigital/master

Disabling the callback for Gesture Recognizers without losing the block
  • Loading branch information...
2 parents 86856ff + ed02c24 commit be6cd4933aebecc093989855fe286cac6cf1bd99 @neror committed Sep 1, 2011
Showing with 36 additions and 18 deletions.
  1. +2 −0 .gitignore
  2. +21 −12 Classes/FTUtils+UIGestureRecognizer.m
  3. +13 −6 Headers/FTUtils/FTUtils+UIGestureRecognizer.h
View
@@ -5,3 +5,5 @@ build
/apidocs/html
/apidocs/docset
/apidocs/publish
+*.xcworkspace
+*xcuserdata
@@ -20,7 +20,7 @@ of this software and associated documentation files (the "Software"), to deal
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-*/
+ */
#if NS_BLOCKS_AVAILABLE
@@ -33,33 +33,42 @@ - (void)handleAction:(UIGestureRecognizer *)recognizer;
@end
-static char * kFTGestureActionKey = "ft_gestureAction";
+static char * kFTGestureActionKey = "ft_gestureAction";
+static char * kFTGestureDisabledKey = "ft_gestureDisabled";
@implementation UIGestureRecognizer(FTBlockAdditions)
+ (id)recognizer {
- return [self recognizerWithActionBlock:nil];
+ return [self recognizerWithActionBlock:nil];
}
+ (id)recognizerWithActionBlock:(FTGestureActionBlock)action {
- id me = [[self class] alloc];
- me = [me initWithTarget:me action:@selector(handleAction:)];
- [me setActionBlock:action];
- return [me autorelease];
+ id me = [[self class] alloc];
+ me = [me initWithTarget:me action:@selector(handleAction:)];
+ [me setActionBlock:action];
+ return [me autorelease];
}
- (void)handleAction:(UIGestureRecognizer *)recognizer {
- if(self.actionBlock) {
- self.actionBlock(recognizer);
- }
+ if(self.actionBlock && !self.disabled) {
+ self.actionBlock(recognizer);
+ }
}
- (FTGestureActionBlock)actionBlock {
- return objc_getAssociatedObject(self, kFTGestureActionKey);
+ return objc_getAssociatedObject(self, kFTGestureActionKey);
}
- (void)setActionBlock:(FTGestureActionBlock)actionBlock {
- objc_setAssociatedObject(self, kFTGestureActionKey, actionBlock, OBJC_ASSOCIATION_COPY);
+ objc_setAssociatedObject(self, kFTGestureActionKey, actionBlock, OBJC_ASSOCIATION_COPY);
+}
+
+- (BOOL)disabled {
+ return [objc_getAssociatedObject(self, kFTGestureDisabledKey) boolValue];
+}
+
+- (void)setDisabled:(BOOL)disabled {
+ objc_setAssociatedObject(self, kFTGestureDisabledKey, [NSNumber numberWithBool:disabled], OBJC_ASSOCIATION_COPY);
}
@end
@@ -20,7 +20,7 @@
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-*/
+ */
#if NS_BLOCKS_AVAILABLE
@@ -34,7 +34,7 @@ typedef void (^FTGestureActionBlock)(id recognizer);
[Event Handling Guide for iOS][1].
[1]:http://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html
-*/
+ */
@interface UIGestureRecognizer(FTBlockAdditions)
#pragma mark - Creating a block based Gesture Recognizer
@@ -51,7 +51,7 @@ typedef void (^FTGestureActionBlock)(id recognizer);
@return An instance of a `UIGestureRecognizer` subclass.
@see actionBlock
-*/
+ */
+ (id)recognizer;
/**
@@ -61,7 +61,7 @@ typedef void (^FTGestureActionBlock)(id recognizer);
@return An instance of a `UIGestureRecognizer` subclass.
@param action A block which will handle the gesture actions.
@see actionBlock
-*/
+ */
+ (id)recognizerWithActionBlock:(FTGestureActionBlock)action;
#pragma mark - Setting and getting the action handler blocks
@@ -72,12 +72,19 @@ typedef void (^FTGestureActionBlock)(id recognizer);
/**
A block to be executed when a `UIGestureRecognizer` action is fired.
-
+
The block is passed a single parameter which is the `UIGestureRecognizer`
instance for this property.
-*/
+ */
@property (copy) FTGestureActionBlock actionBlock;
+/**
+ A property indicating that the block should *not* be called when
+ the recognizer fires. Useful if you need to temporarily disable an action
+ but you still want the block to be around later on.
+ */
+@property (nonatomic, assign) BOOL disabled;
+
@end
#endif

0 comments on commit be6cd49

Please sign in to comment.