UIScrollView子视图中的UITapGestureRecognizer

时间:2011-03-19 18:11:42

标签: objective-c uiscrollview uiimageview uigesturerecognizer

我正在尝试在imageView中使用单击识别器(也是一个scrollView子项)。 在Interface Builder中,我只创建并引用了scrollView。 滚动工作,但无法识别单击事件(没有记录任何内容)。 这是代码:

- (void)loadView {
[super loadView];

UIImage *myImage = [UIImage imageNamed:@"img1.jpg"];
imageView = [[UIImageView alloc] initWithImage:myImage];

[myImage release];

// add gesture recognizers to the image view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

[imageView addGestureRecognizer:singleTap];

[singleTap release];

[imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];

[imageScrollView addSubview:imageView]; }

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
// single tap handling
NSLog(@"sinlgeTap called"); }

我环顾了几个小时,尝试了很多东西。也许看别人可以帮助更多。 感谢。

1 个答案:

答案 0 :(得分:31)

我为imageView启用了userInteraction,并为scrollView启用了canCancelContentTouches选项:

[imageScrollView setCanCancelContentTouches:YES];
[imageView setUserInteractionEnabled:YES];

现在有效。