开发者

Gesture recognizer blocked by section index of table view?

开发者 https://www.devze.com 2023-03-03 14:10 出处:网络
I have a custom gesture recognizer in my application, and add it to window object, so it can detect custom gesture in every scene of my application. I found it works fine except finger touch on table

I have a custom gesture recognizer in my application, and add it to window object, so it can detect custom gesture in every scene of my application. I found it works fine except finger touch on table view's section index.

I setup a simple test project, only single table view in window:

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:Cell开发者_JAVA技巧Identifier] 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;
}

Touch everywhere in table view, log output is normal. But if touch on table view's section index, nothing can be found in log. In my understand, gesture recognizer of the hit test view and the view's ancestor view will receive touch event, so gesture recognizer of window can receive all touch event on all views. And in MyGestureRecognizer, -(BOOL)canBePreventedByGestureRecognizer: return NO, so no gesture recognizers can prevent MyGestureRecognizer. My understand is wrong?


I wonder if you might be hitting a variation of the problem I had here

which might happen depending on how the table view has it's internal views stacked.

0

精彩评论

暂无评论...
验证码 换一张
取 消