Double Tapped Gesture无法使用addGestureRecognizer:

时间:2013-05-14 04:42:06

标签: ios objective-c uigesturerecognizer

我在ImageView中有一个ImageView我显示了一些图像,但当我在那些图像上点击2次时,我将无法获得正确的事件。

此方法未调用。

- (void)doubleTapWebView:(UITapGestureRecognizer *)gesture
{
    NSLog(@"double-tap");
    // nothing to do here
}

我试过这段代码:

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapWebView:)];
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.delegate = self;
    [self.ImgView addGestureRecognizer:doubleTap];


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

这是代码的其余部分:

UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    [scrollView setPagingEnabled:YES];

    [scrollView setShowsHorizontalScrollIndicator:NO];

    FrontsCards=[[NSMutableArray alloc]initWithObjects:@"cloub1.png",@"cloub2.png",@"cloub3.png",@"cloub4.png", nil];

    for(int m=0; m< [FrontsCards count];m++)
    {

    //  int randIdx=arc4random()%[FrontsCards count];

        NSString *imageName=[FrontsCards objectAtIndex:m];

        NSString *fullImageName=[NSString stringWithFormat:@"%@",imageName];

        int padding=25;
        // padding is given.

        CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);

        ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];

        [ImgView setImage:[UIImage imageNamed:fullImageName]];

        [ImgView setContentMode:UIViewContentModeScaleAspectFill];

        [scrollView addSubview:ImgView];
    }

    CGSize scrollViewSize=CGSizeMake(scrollView.frame.size.width*[FrontsCards count], scrollView.frame.size.height);

    [scrollView setContentSize:scrollViewSize];

    [self.view addSubview:scrollView];

3 个答案:

答案 0 :(得分:2)

默认情况下,imageview userInteractio为false,因此您可以添加此行代码。

self.ImgView.userInteractionEnabled=YES;

在.h文件中添加UIGestureRecognizerDelegate

答案 1 :(得分:2)

默认情况下,图片用户互动为NO。所以把它YES

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapWebView:)];
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.delegate = self;
    [self.ImgView addGestureRecognizer:doubleTap];
self.ImgView.userInteractionEnabled=YES;

答案 2 :(得分:1)

在循环中,您将四个图像视图中的每一个分配给同一个实例变量(ImgView)。

在循环结束时,ImgView将指向最后一个,因此这是添加手势识别器的那个。如果要在每个图像视图上响应双击事件,则必须为每个图像视图添加单独的手势识别器。最简单的方法是在循环中添加识别器。

另外,正如其他人已经指出的那样,您需要将userInteractionEnabled设置为YES才能使手势识别器处理图像视图。

顺便说一句,当你迭代数组的元素(或一般的集合)时,不需要计数器变量,只需使用for (NSString *imageName in FrontCards) { ... }。并且该变量的名称应为frontCards;通常为类名保留UpperCase名称。