A count of started touches is not equal to count of finished touches

后端 未结 5 2089
无人共我
无人共我 2021-02-07 10:10

I have the following code for testing purposes:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self customTouchHandler:touches];
}

- (v         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-07 10:24

    Also have this problem.

    Only solution i found at this moment based on observing touch phase:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"Began %lu of %lu", [touches count], [event.allTouches count]);
    
        for (UITouch *touch in touches) {
            [touch addObserver:self forKeyPath:@"phase" options:0 context:nil];
        }
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"Ended %lu of %lu", [touches count], [event.allTouches count]);
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
        if ([object isKindOfClass:[UITouch class]]) {
            UITouch *touch = object;
            NSLog(@"Touch %lu phase: %ld", (unsigned long)[touch hash], [touch phase]);
            if (touch.phase == UITouchPhaseEnded || touch.phase == UITouchPhaseCancelled) {
                NSLog(@"Touch %lu ended", (unsigned long)[touch hash]);
                [touch removeObserver:self forKeyPath:@"phase"];
            }
        }
    }
    

提交回复
热议问题