手势识别器被表视图的段索引阻止?

时间:2011-05-08 15:09:08

标签: iphone uigesturerecognizer

我的应用程序中有一个自定义手势识别器,并将其添加到窗口对象,因此它可以在我的应用程序的每个场景中检测自定义手势。我发现除了手指触摸表视图的部分索引外,它工作正常。

我设置了一个简单的测试项目,在窗口中只有单个表视图:

TestGestureAppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self.window addSubview:_rootViewController.view];
    MyGestureRecognizer *recognizer = [[MyGestureRecognizer alloc] initWithTarget:nil action:nil];
    [self.window addGestureRecognizer:recognizer];
    [recognizer release];

    [self.window makeKeyAndVisible];
    return YES;
}

MyGestureRecognizer:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"GestureRecognizer touchesBegan");
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"GestureRecognizer touchesMoved");
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"GestureRecognizer touchesEnded");
    [super touchesEnded:touches withEvent:event];
    self.state = UIGestureRecognizerStateFailed;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"GestureRecognizer touchesCancelled");
    [super touchesCancelled:touches withEvent:event];
    self.state = UIGestureRecognizerStateFailed;
}

- (void)reset {
    NSLog(@"GestureRecognizer reset");
    [super reset];
}

- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer {
    NSLog(@"GestureRecognizer canBePreventedByGestureRecognizer:%@", preventingGestureRecognizer);
    return NO;
}

RootViewController的:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%c - %d", ('A' + indexPath.section), indexPath.row];

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"%c", ('A' + section)];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", nil];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return index;
}

在表格视图中触摸到处,日志输出正常。但是如果触摸表视图的节索引,则在日志中找不到任何内容。 据我所知,命中测试视图的手势识别器和视图的祖先视图将接收触摸事件,因此窗口的手势识别器可以接收所有视图上的所有触摸事件。而在MyGestureRecognizer中, - (BOOL)canBePreventedByGestureRecognizer:返回NO,因此没有手势识别器可以阻止MyGestureRecognizer。我的理解是错的?

1 个答案:

答案 0 :(得分:0)

我想知道你是否可能遇到问题的变化here

可能会发生这种情况,具体取决于表格视图如何堆叠内部视图。