From 51c280c8fd78156c3b0bae4278bbe34099fe77ad Mon Sep 17 00:00:00 2001 From: Anas Ambri Date: Mon, 27 Oct 2014 17:28:11 -0400 Subject: [PATCH] Add UITapGestureRecognizer on the SWActionSheet to allow tracking of taps. This also permits dismissing the action sheet when clicking on the background. The check for which view has been clicked is pretty rudimentary, so I would be interested if someone can come up with something better. Closes #80 --- Pickers/SWActionSheet.m | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Pickers/SWActionSheet.m b/Pickers/SWActionSheet.m index 6179c5824..d4f43b38c 100644 --- a/Pickers/SWActionSheet.m +++ b/Pickers/SWActionSheet.m @@ -102,10 +102,19 @@ - (instancetype)initWithView:(UIView *)aView _bgView.backgroundColor = [UIColor whiteColor]; [self addSubview:_bgView]; [self addSubview:view]; + UITapGestureRecognizer * backgroundTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissActionSheet:)]; + [self addGestureRecognizer:backgroundTapRecognizer]; } return self; } +- (void) dismissActionSheet:(UITapGestureRecognizer*) sender { + UIView *tappedView = [view hitTest:[sender locationInView:view] withEvent:nil]; + if (self && self.presented && tappedView == nil) { + [self dismissWithClickedButtonIndex:0 animated:YES]; + } +} + - (void)configureFrameForBounds:(CGRect)bounds { self.frame = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height + view.bounds.size.height);