Objective-c:如何在视图上检测双击?

时间:2011-09-06 07:11:32

标签: iphone objective-c ios ipad

我正在开发一个应用程序,我在视图上有多个控件,但我想在用户双击视图时启用它们

您可以采用双击的示例,但在设备中,我想在双击时捕捉到该事件。

2 个答案:

答案 0 :(得分:77)

您需要在要点按的视图中添加UITapGestureRecognizer

像这样:

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:tapGesture];
    [tapGesture release];
}

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateRecognized) {
        // handling code
    }
}

答案 1 :(得分:8)

使用UITapGestureRecognizer在视图中添加numberOfTapsRequired = 2