Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect a tap anywhere in the view?

I have a tutorial for my app, which should display only the first time the app is opened and should be tapped to dismiss.

I am initializing a UITapGestureRecognizer in my viewDidLoad:

tapper_tut = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapper_tut.cancelsTouchesInView = FALSE;
[self.view addGestureRecognizer:tapper_tut];

and I have an IBAction to detect the tap and set the tutorial to hidden:

- (IBAction)dismiss_tut{
    if (????????????????) {
        _tutorial.hidden = YES;
    }
}

But I have no idea what to put in the if statement condition, or if this is even that right way to go about this.

How would I dismiss a UIImageView on a tap?

like image 864
ZuluDeltaNiner Avatar asked Dec 03 '22 21:12

ZuluDeltaNiner


2 Answers

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:gr];
// if not using ARC, you should [gr release];
// mySensitiveRect coords are in the coordinate system of self.view


- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.view];
    if (CGRectContainsPoint(mySensitiveRect, p)) {
        NSLog(@"got a tap in the region i care about");
    } else {
        NSLog(@"got a tap, but not where i need it");
    }
}
like image 182
Jay Gajjar Avatar answered Dec 28 '22 07:12

Jay Gajjar


You can make viewDidLoad like this

- (void)viewDidLoad 
  { 
      [super viewDidLoad];
      self.view.backgroundColor = [UIColor whiteColor];

      /* Create the Tap Gesture Recognizer */
      self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                   action:@selector(handleTaps:)]; 

     /* The number of fingers that must be on the screen */
      self.tapGestureRecognizer.numberOfTouchesRequired = 1;

     /* The total number of taps to be performed before the gesture is recognized */
      self.tapGestureRecognizer.numberOfTapsRequired = 1;

     /* Add this gesture recognizer to the view */
     [self.view addGestureRecognizer:self.tapGestureRecognizer];
  }

To detect the taps you can make the method like this.

- (void) handleTaps:(UITapGestureRecognizer*)paramSender
  {
      NSUInteger touchCounter = 0; 
      for (touchCounter = 0;touchCounter < paramSender.numberOfTouchesRequired;touchCounter++)
      {
            CGPoint touchPoint =[paramSender locationOfTouch:touchCounter inView:paramSender.view];
            NSLog(@"Touch #%lu: %@",(unsigned long)touchCounter+1, NSStringFromCGPoint(touchPoint));
      }
  }
like image 35
Bhavesh Avatar answered Dec 28 '22 08:12

Bhavesh