Skip to content

Commit

Permalink
Merge pull request mutualmobile#20 from mutualmobile/pan_callback_block
Browse files Browse the repository at this point in the history
Support for a callback block when a gesture event is completed.
  • Loading branch information
kcharwood committed May 30, 2013
2 parents 23d0152 + 341b926 commit d37b0ac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
13 changes: 13 additions & 0 deletions MMDrawerController/MMDrawerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,17 @@ typedef void (^MMDrawerControllerDrawerVisualStateBlock)(MMDrawerController * dr
*/
-(void)setDrawerVisualStateBlock:(void(^)(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible))drawerVisualStateBlock;

///---------------------------------------
/// @name Gesture Completion Handling
///---------------------------------------

/**
Sets a callback to be called when a gesture has been completed.
This block is called when a gesture action has been completed. You can query the `openSide` of the `drawerController` to determine what the new state of the drawer is.
@param gestureCompletionBlock A block object to be called that allows the implementer be notified when a gesture action has been completed.
*/
-(void)setGestureCompletionBlock:(void(^)(MMDrawerController * drawerController, UIGestureRecognizer * gesture))gestureCompletionBlock;

@end
20 changes: 18 additions & 2 deletions MMDrawerController/MMDrawerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
/** The percent of the possible overshoot width to use as the actual overshoot percentage. */
CGFloat const MMDrawerOvershootPercentage = 0.1f;

typedef void (^MMDrawerGestureCompletionBlock)(MMDrawerController * drawerController, UIGestureRecognizer * gesture);

static CAKeyframeAnimation * bounceKeyFrameAnimationForDistanceOnView(CGFloat distance, UIView * view) {
CGFloat factors[32] = {0, 32, 60, 83, 100, 114, 124, 128, 128, 124, 114, 100, 83, 60, 32,
0, 24, 42, 54, 62, 64, 62, 54, 42, 24, 0, 18, 28, 32, 28, 18, 0};
Expand Down Expand Up @@ -120,6 +122,7 @@ @interface MMDrawerController () <UIGestureRecognizerDelegate>{

@property (nonatomic, assign) CGRect startingPanRect;
@property (nonatomic, copy) MMDrawerControllerDrawerVisualStateBlock drawerVisualState;
@property (nonatomic, copy) MMDrawerGestureCompletionBlock gestureCompletion;

@end

Expand Down Expand Up @@ -524,6 +527,11 @@ -(void)setDrawerVisualStateBlock:(void (^)(MMDrawerController *, MMDrawerSide, C
[self setDrawerVisualState:drawerVisualStateBlock];
}

#pragma mark - Setting the Gesture Completion Block
-(void)setGestureCompletionBlock:(void (^)(MMDrawerController *, UIGestureRecognizer *))gestureCompletionBlock{
[self setGestureCompletion:gestureCompletionBlock];
}

#pragma mark - Subclass Methods
-(BOOL)shouldAutomaticallyForwardAppearanceMethods{
return NO;
Expand Down Expand Up @@ -737,7 +745,11 @@ -(CGFloat)visibleRightDrawerWidth{

-(void)tapGesture:(UITapGestureRecognizer *)tapGesture{
if(self.openSide != MMDrawerSideNone){
[self closeDrawerAnimated:YES completion:nil];
[self closeDrawerAnimated:YES completion:^(BOOL finished) {
if(self.gestureCompletion){
self.gestureCompletion(self, tapGesture);
}
}];
}
}

Expand Down Expand Up @@ -788,7 +800,11 @@ -(void)panGesture:(UIPanGestureRecognizer *)panGesture{
case UIGestureRecognizerStateEnded:{
self.startingPanRect = CGRectNull;
CGPoint velocity = [panGesture velocityInView:self.view];
[self finishAnimationForPanGestureWithXVelocity:velocity.x completion:nil];
[self finishAnimationForPanGestureWithXVelocity:velocity.x completion:^(BOOL finished) {
if(self.gestureCompletion){
self.gestureCompletion(self, panGesture);
}
}];
break;
}
default:
Expand Down

0 comments on commit d37b0ac

Please sign in to comment.