Detect single tap on UIScrollView containing 'n' UIImageViews
Asked Answered
S

1

6

I am simply trying to get hold of the UIImageView that was tapped on, from the UIScrollView.

I have found 2 ways of achieving the above on the Web.

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

This method has not worked for me. the handleSingleTap method never gets called.

I am out of ideas on what I am doing wrong/why this is not working.

    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!!!");
            }

2nd Method : subclass 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 touchesDown responder does get called but the '[touches anyobject]' isn't the UIImageView which was tapped on.

I tried setting the 'tag' on every UIImageView I am adding to the scrollview with an increasing counter but I get back 0 no matter which imageview i tap on.

I am new to cocoa in general and I am not sure how to leverage this responder in any other fashion.

Any suggestions/hints would be great.

thanks in advance.

Shanleigh answered 3/5, 2011 at 21:9 Comment(0)
U
12

did you set the userInteractionEnabled=YES on the UIImageView. Its turned off by default.

Upheave answered 3/5, 2011 at 21:18 Comment(5)
Whoa!!. Jason, Thanks a bunch. I had no idea about this. That helps!!!. I was creating 'n' uiimageviewers in code. Is this standard on all controls in cocoa?Shanleigh
no, normally turned on. Just a few of the typically non interaction views have it turned off. UILabel, UIImageView are the two that come to mind.Upheave
it would make sense that if someone added a UIGestureRecognizer to a UIView, that the userInterationEnabled would automatically flip to ON.. but then again, no one from Apple ever asks my opinion. :-)Upheave
It is a steep curve coming fromm .Net. things like getting the tapped/selected item are built into the api. cocoa seems to want to make you hand code as much as possible.Shanleigh
agreed. thats the same route I came from as well.Upheave

© 2022 - 2024 — McMap. All rights reserved.