Pen click gesture with iphone / ipad argument

when my gesture of clicking the arrow, I need to send an additional argument along with it, but I have to do something really stupid, which I am doing wrong here:

Here is my gesture created and added:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:itemSKU:)]; tapGesture.numberOfTapsRequired=1; [imageView setUserInteractionEnabled:YES]; [imageView addGestureRecognizer:tapGesture]; [tapGesture release]; [self.view addSubview:imageView]; 

This is where I process it:

 -(void) handleTapGesture:(UITapGestureRecognizer *)sender withSKU: (NSString *) aSKU { NSLog(@"SKU%@\n", aSKU); } 

and this will not be executed due to the initialization string of UITapGestureRecognizer.

I need to know something identifiable about which image was clicked.

+4
source share
1 answer

the gesture recognizer will pass only one argument to the action selector: itself. I assume that you are trying to distinguish cranes in different images in the main view? In this case, it is best to call -locationInView: pass the supervisor, and then call -hitTest:withEvent: in this view with the resulting CGPoint . In other words, something like this:

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; ... - (void)imageTapped:(UITapGestureRecognizer *)sender { UIView *theSuperview = self.view; // whatever view contains your image views CGPoint touchPointInSuperview = [sender locationInView:theSuperview]; UIView *touchedView = [theSuperview hitTest:touchPointInSuperview withEvent:nil]; if([touchedView isKindOfClass:[UIImageView class]]) { // hooray, it one of your image views! do something with it. } } 
+12
source

Source: https://habr.com/ru/post/1314471/


All Articles