ios – 为什么是hitTest:withEvent:每次触摸三次?

我有一个UIView的子类,其中我覆盖了hitTest:withEvent:如下:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"Event = %@",event);
    return self;
}

对于视图中的每个触摸,我看到三次打电话给hitTest:withEvent :.这三个电话是在接触之前进行的.输出如下:

2011-07-01 09:20:58.553 AppName[930:207] Event = <UItouchesEvent: 0x6a08360> timestamp: 4297.16 touches: {(
)}
2011-07-01 09:20:58.553 AppName[930:207] Event = <UItouchesEvent: 0x6a08360> timestamp: 4297.16 touches: {(
)}
2011-07-01 09:20:58.554 AppName[930:207] Event = <UItouchesEvent: 0x6a08360> timestamp: 4304.54 touches: {(
)}

基于时间戳和地址,似乎似乎正在使用单个UItouchesEvent对象,并且其时间戳在第三次调用之前未正确设置.任何人都可以解释为什么hitTest:withEvent:得到这样叫三次?我不是在寻找解决方法.我只想了解发生了什么

解决方法

我有同样的问题,并能用这个代码解决它.即使pointInside和hitTest被调用了3次,触摸的UIView的touchesBegan(或touchesEnded)只会被调用一次.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
    if (event.type == UIEventTypetouches)
        NSLog(@"%@",self);
}


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if ([self pointInside:point withEvent:event])
        return self;

    return [super hitTest:point withEvent:event];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if (CGRectContainsPoint([self bounds],point))
    {
        if (event.type == UIEventTypetouches)
        {           
            return YES;
        }
    }

    return NO;
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...