Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tap gesture recognizer - which object was tapped?

I'm new to gesture recognizers so maybe this question sounds silly: I'm assigning tap gesture recognizers to a bunch of UIViews. In the method is it possible to find out which of them was tapped somehow or do I need to find it out using the point that was tapped on screen?

for (NSUInteger i=0; i<42; i++) {         float xMultiplier=(i)%6;         float yMultiplier= (i)/6;         float xPos=xMultiplier*imageWidth;         float yPos=1+UA_TOP_WHITE+UA_TOP_BAR_HEIGHT+yMultiplier*imageHeight;         UIView *greyRect=[[UIView alloc]initWithFrame:CGRectMake(xPos, yPos, imageWidth, imageHeight)];         [greyRect setBackgroundColor:UA_NAV_CTRL_COLOR];          greyRect.layer.borderColor=[UA_NAV_BAR_COLOR CGColor];         greyRect.layer.borderWidth=1.0f;         greyRect.userInteractionEnabled=YES;         [greyGridArray addObject:greyRect];         [self.view addSubview:greyRect];         NSLog(@"greyGrid: %i: %@", i, greyRect);          //make them touchable         UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter)];         letterTapRecognizer.numberOfTapsRequired = 1;         [greyRect addGestureRecognizer:letterTapRecognizer];     } 
like image 484
suMi Avatar asked Feb 05 '14 09:02

suMi


People also ask

What is tap gesture in Swift?

When one of these objects recognizes a common gesture or, in some cases, a change in the gesture, it sends an action message to each designated target object. The concrete subclasses of UIGestureRecognizer are Tap Gesture Recognizer.

What is tap gesture?

A discrete gesture that recognizes one or many taps. Tap gestures detect one or more fingers briefly touching the screen. The fingers involved in these gestures must not move significantly from their initial touch positions.

How do I use tap gestures in Swift 4?

Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

What is UITapGestureRecognizer Swift?

UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.


1 Answers

Define your target selector(highlightLetter:) with argument as

UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter:)]; 

Then you can get view by

- (void)highlightLetter:(UITapGestureRecognizer*)sender {      UIView *view = sender.view;       NSLog(@"%d", view.tag);//By tag, you can find out where you had tapped.  } 
like image 154
Mani Avatar answered Sep 20 '22 22:09

Mani