Declared in UIGestureRecognizer+BlocksKit.h

Overview

Block functionality for UIGestureRecognizer.

Use of the delay property is pretty straightforward, although cancellation might be a little harder to swallow. An example follows: UITapGestureRecognizer *singleTap = [UITapGestureRecognizer recognizerWithHandler:^(id sender) { NSLog(@“Single tap.”); } delay:0.18]; [self addGestureRecognizer:singleTap];

 UITapGestureRecognizer *doubleTap = [UITapGestureRecognizer recognizerWithHandler:^(id sender) {
    [singleTap cancel];
    NSLog(@"Double tap.");
 }];
 doubleTap.numberOfTapsRequired = 2;
 [self addGestureRecognizer:doubleTap];

Believe it or not, the above code is fully memory-safe and efficient. Eagle-eyed coders will notice that this setup emulates UIGestureRecognizer’s requireGestureRecognizerToFail:, and, yes, it totally apes it. Not only is this setup much faster on the user’s end of things, it is more flexible and allows for much more complicated setups.

Includes code by the following:

Warning: UIGestureRecognizer is only available on a platform with UIKit.

Warning: It is not recommended to use the Apple-supplied locationInView and state methods on a delayed block-backed gesture recognizer, as these properties are likely to have been cleared by the time by the block fires. It is instead recommended to use the arguments provided to the block.

Tasks

  • + bk_recognizerWithHandler:delay:

    An autoreleased gesture recognizer that will, on firing, call the given block asynchronously after a number of seconds.

  • – bk_initWithHandler:delay:

    Initializes an allocated gesture recognizer that will call the given block after a given delay.

  • + bk_recognizerWithHandler:

    An autoreleased gesture recognizer that will call the given block.

  • – bk_initWithHandler:

    Initializes an allocated gesture recognizer that will call the given block.

  •   bk_handler

    Allows the block that will be fired by the gesture recognizer to be modified after the fact.

    property
  •   bk_handlerDelay

    Allows the length of the delay after which the gesture recognizer will be fired to modify.

    property
  • – bk_cancel

    If the recognizer happens to be fired, calling this method will stop it from firing, but only if a delay is set.

Properties

bk_handler

@property (nonatomic, copy, setter=bk_setHandler:) void ( ^ ) ( UIGestureRecognizer *sender , UIGestureRecognizerState state , CGPoint location ) bk_handler
Discussion

Allows the block that will be fired by the gesture recognizer to be modified after the fact.

Declared In

UIGestureRecognizer+BlocksKit.h

bk_handlerDelay

@property (nonatomic, setter=bk_setHandlerDelay:) NSTimeInterval bk_handlerDelay
Discussion

Allows the length of the delay after which the gesture recognizer will be fired to modify.

Declared In

UIGestureRecognizer+BlocksKit.h

Class Methods

bk_recognizerWithHandler:

+ (id)bk_recognizerWithHandler:(void ( ^ ) ( UIGestureRecognizer *sender , UIGestureRecognizerState state , CGPoint location ))block
Discussion

An autoreleased gesture recognizer that will call the given block.

For convenience and compatibility reasons, this method is indentical to using recognizerWithHandler:delay: with a delay of 0.0.

Parameters

block

The block which handles an executed gesture.

Return Value

An initialized and autoreleased instance of a concrete UIGestureRecognizer subclass, or nil.

Declared In

UIGestureRecognizer+BlocksKit.h

bk_recognizerWithHandler:delay:

+ (id)bk_recognizerWithHandler:(void ( ^ ) ( UIGestureRecognizer *sender , UIGestureRecognizerState state , CGPoint location ))block delay:(NSTimeInterval)delay
Discussion

An autoreleased gesture recognizer that will, on firing, call the given block asynchronously after a number of seconds.

Parameters

block

The block which handles an executed gesture.

delay

A number of seconds after which the block will fire.

Return Value

An autoreleased instance of a concrete UIGestureRecognizer subclass, or nil.

Declared In

UIGestureRecognizer+BlocksKit.h

Instance Methods

bk_cancel

- (void)bk_cancel
Discussion

If the recognizer happens to be fired, calling this method will stop it from firing, but only if a delay is set.

Warning: This method is not for arbitrarily canceling the firing of a recognizer, but will only function for a block handler after the recognizer has already been fired. Be sure to make your delay times accomodate this likelihood.

Declared In

UIGestureRecognizer+BlocksKit.h

bk_initWithHandler:

- (id)bk_initWithHandler:(void ( ^ ) ( UIGestureRecognizer *sender , UIGestureRecognizerState state , CGPoint location ))block
Discussion

Initializes an allocated gesture recognizer that will call the given block.

This method is indentical to calling initWithHandler:delay: with a delay of 0.0.

Parameters

block

The block which handles an executed gesture.

Return Value

An initialized instance of a concrete UIGestureRecognizer subclass or nil.

Declared In

UIGestureRecognizer+BlocksKit.h

bk_initWithHandler:delay:

- (id)bk_initWithHandler:(void ( ^ ) ( UIGestureRecognizer *sender , UIGestureRecognizerState state , CGPoint location ))block delay:(NSTimeInterval)delay
Discussion

Initializes an allocated gesture recognizer that will call the given block after a given delay.

An alternative to the designated initializer.

Parameters

block

The block which handles an executed gesture.

delay

A number of seconds after which the block will fire.

Return Value

An initialized instance of a concrete UIGestureRecognizer subclass or nil.

Declared In

UIGestureRecognizer+BlocksKit.h