Single click detection on a UIScrollView containing "n" UIImageViews

I'm just trying to get the UIImageView that was involved from the UIScrollView.

I found two ways to achieve the above on the Internet.

1st method: create a tap gesture on the uiimageview screen before adding it to the scrollviewer.

This method did not work for me. The handleSingleTap method is never called.

I have no idea what I am doing wrong / why it does not work.

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; singleTap.numberOfTapsRequired = 1; [imageView addGestureRecognizer:singleTap]; [singleTap release]; [framesSourceScrollview addSubview:imageView]; [imageView release]; - (void)handleSingleTap:(UIGestureRecognizer *)sender { NSLog(@"image tapped!!!"); } 

Second method: subclass of UIScrollView

 @interface SingleTapScrollViewer : UIScrollView { } @end @implementation SingleTapScrollViewer - (id)initWithFrame:(CGRect)frame { return [super initWithFrame:frame]; } - (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event { // If not dragging, send event to next responder if (!self.dragging) [self.nextResponder touchesEnded: touches withEvent:event]; else [super touchesEnded: touches withEvent: event]; } @end In the ViewController - (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event { // Process the single tap here NSLog(@"Scroll view single tapped. touches count : %d", touches.count); UITouch *touch = [touches anyObject]; UIImageView *imgView = (UIImageView*)touch.view; NSLog(@"tag is %@", imgView.tag); } 

Using this method, the calling touchsDown response calls the call, but '[concerns anyobject]' is not the UIImageView that was used.

I tried installing a "tag" on each UIImageView. I add to the scrollview with an increasing counter, but I return 0 no matter what image I click on.

I'm new to cocoa in general, and I'm not sure how to use this responder in any other way.

Any suggestions / tips would be great.

early.

+6
source share
1 answer

You set userInteractionEnabled = YES in UIImageView. By default it is disabled.

+12
source

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


All Articles