在iOS上,为什么以下UITapGestureRecognizer(Tap)不起作用?

时间:2012-05-21 12:17:31

标签: ios cocoa-touch uiview uiviewcontroller

如果我只是在Xcode 4.3.2上启动一个全新的Single View App项目,请使用以下代码:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] 
                              initWithImage:[UIImage imageNamed:@"pic.jpg"]];
    [self.view addSubview:imageView];

    self.view.userInteractionEnabled = YES;
    self.view.multipleTouchEnabled = YES;

    UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] 
                               initWithTarget:self action:@selector(handleTap:)];
    tapRecognizer.numberOfTapsRequired = 1;

    [imageView addGestureRecognizer:tapRecognizer];
}

-(void) handleTap:(UIGestureRecognizer *) gesture {
    NSLog(@"Image tapped on");
}

即使我点击图片,也永远不会调用handleTap方法......这里的内容是不是正确完成了?

2 个答案:

答案 0 :(得分:4)

您需要在图片视图中启用触摸(默认情况下已禁用),而不是在控制器的视图中:

imageView.userInteractionEnabled = YES;

答案 1 :(得分:0)

头文件中@interface的末尾是<UIGestureRecognizerDelegate>吗?