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;
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...