如何知道UITapGestureRecognizer是否已添加到子视图中

时间:2012-09-21 21:14:11

标签: ios xcode uiscrollview uitapgesturerecognizer

我添加了程序化的子视图。对于每个子视图,我正在添加一个手势重新识别器:

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake((position*1024)+200,0,image.size.width,image.size.height);
    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc]
                                               initWithTarget:self action:@selector(singleFingerTap:)];
    singleFingerTap.numberOfTapsRequired = 1;
    [imageView addGestureRecognizer:singleFingerTap];
    [singleFingerTap release];

但是点击没有响应我如何验证手势是否已将其添加到子视图?

1 个答案:

答案 0 :(得分:3)

在代码后添加:

NSLog(@"imageView.gestureRecognizers: %@", [imageView.gestureRecognizers description]);

如果您已正确添加了gestureRecognizers,它会将每个描述打印到控制台。如果没有,它将在控制台中显示(NULL)或空数组。


您还可以设置手势识别器委托:

[singleFingerTap setDelegate:self];

然后添加委托方法并设置断点以确保它被调用:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"gestureRecognizerShouldBegin: called");
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    NSLog(@"shouldReceiveTouch: called");
    return YES;
}