开发者

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜