我有一个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; }