4

我以编程方式添加子视图。对于每个子视图,我添加了一个手势识别器:

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];

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

4

1 回答 1

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;
}
于 2012-09-21T21:15:39.060 回答