我有一个UIView
的子类,在其中重写了hitTest:withEvent:
,如下所示:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
NSLog(@"Event = %@", event);
return self;
}
对于 View 中的每次触摸,我都会看到对
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:
这样被调用3次?我不是在寻找解决方法。我只想了解发生了什么。 最佳答案
我遇到了同样的问题,并且能够使用此代码解决它。即使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 - 为什么是hitTest :withEvent: called three times for each touch?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6550107/