Skip to content

Commit

Permalink
Can now disable a gesture recognizer action block without setting the…
Browse files Browse the repository at this point in the history
… block to nil
  • Loading branch information
subdigital committed Aug 24, 2011
1 parent d030cfd commit ed02c24
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
33 changes: 21 additions & 12 deletions Classes/FTUtils+UIGestureRecognizer.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
19 changes: 13 additions & 6 deletions Headers/FTUtils/FTUtils+UIGestureRecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -51,7 +51,7 @@ typedef void (^FTGestureActionBlock)(id recognizer);
@return An instance of a `UIGestureRecognizer` subclass.
@see actionBlock
*/
*/
+ (id)recognizer;

/**
Expand All @@ -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
Expand All @@ -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 ed02c24

Please sign in to comment.