Jorge
Jorge

Reputation: 2196

Detecting taps in uiimageview inside uiscrollview

I have a UIScrollView with multiple UIImageViews in it created like this.

frame = [[UIImageView alloc] initWithImage:bg];
frame.frame = CGRectMake(FRAME_SEPARATOR + numPage*1024 + numColumn*(FRAME_SEPARATOR+230), 10 +numRow*(FRAME_SEPARATOR+145), 230, 145);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
[frame addGestureRecognizer:tap];
[tap release];
[scroll addSubView:frame];

The problem is that imageTapped is not being called when tapping over an image.

If I add the gesture recognizer to the scrollview like this:

UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
[scroll addGestureRecognizer:tap];
[tap release];

imageTapped is called.

How can I detect the taps over the UIImageViews?

Thanks

Upvotes: 17

Views: 8938

Answers (1)

user467105
user467105

Reputation:

Make sure userInteractionEnabled is set to YES on the UIImageView:

frame.userInteractionEnabled = YES;

I'd also recommend using a different name for the UIImageView variable (eg. imageView instead of frame). Otherwise, you can easily confuse it with the view's frame property.

Upvotes: 46

Related Questions