1

I have come across both MBProgressHUD and DSActivityView to show the black rounded corner "Loading" type overlays on an iPhone app.

I am just wondering if anyone knows how to extend either of these to detect a tap of the overlay, so that the action can be cancelled.

I have seen at least one app out there which has the "Loading" indicator. But with the text "Tap to cancel".

Thanks!

woopsicle
  • 91
  • 1
  • 2
  • 3

2 Answers2

27
- (void)showHUDWithCancel:(NSString *)aMessage {
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = aMessage;
    self.HUD.detailsLabelText = @"Tap to cancel";
    [self.HUD addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hudWasCancelled)]];
}

- (void)hudWasCancelled {
    [self.HUD hide:YES];
}
user1032657
  • 2,451
  • 6
  • 28
  • 38
  • when I tap to cancel it throws an Exception with error message "unrecognized selector sent to instance" though I'm pretty sure I set hudWasCancelled correctly in the target – Bruce Jul 18 '15 at 15:19
  • Nvm figured out it was due to the missing (id)sender parameter for hudWasCancelled – Bruce Jul 18 '15 at 15:43
  • It takes tap on whole screen, I only want to hide when user tap on Tap to cancel button. Any solution ? – Aleem Nov 28 '18 at 13:26
1

The easiest way to do this is to add a gesture recogniser (for a single tap) to the relevant view (in the case of MBProgressHUD, this can be the class itself, since MBProgressHUD is a subclass of UIView). Upon detecting the tap, you can trigger the dismiss method ([MBProgress HUD hide]).

You'll probably also want to trigger a NSNotification of some sort for your app to pick up on, because presumably in addition to removing the loading view itself you'll also want to cancel and clean up the operation you were performing during the load.

lxt
  • 31,146
  • 5
  • 78
  • 83