在UIImageView上点击手势事件不会被触发

时间:2012-09-17 23:59:15

标签: objective-c ios

我正在尝试在UIImageView上实现一个tap-gesture识别器。但是我无法获得处理tap事件的方法。我已将我的代码与stackoverflow上许多类似问题的答案进行了比较,但问题仍然存在。以下是我的申请中的相关代码。有人能告诉我我做错了吗?

ViewController.h

IBOutlet UIImageView *topLeftImage;

-(IBAction)handleSingleTap:(UITapGestureRecognizer*)gestureView;

ViewController.m

- (void)viewDidUnload
{
    [super viewDidUnload];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    topLeftImage.userInteractionEnabled = YES;
    [topLeftImage addGestureRecognizer:singleTap];

}

- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer {

    NSLog(@"Tap detected.  Booyah!");
}

1 个答案:

答案 0 :(得分:5)

试试这个。你把你的代码放在viewDidUnload中,完全错误的地方。你想要viewDidLoad。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    topLeftImage.userInteractionEnabled = YES;
    [topLeftImage addGestureRecognizer:singleTap];

}