我有一个自定义窗口(应该在所有内容之上,包括键盘)来显示叠加层,就像您在设备中按下音量增大/减小按钮时看到的叠加层。
所以我做了一个自定义窗口 OverlayWindow 到目前为止一切正常,后面的窗口正常接收它们的事件。然而hitTest:withEvent:
,它被多次调用,有时它甚至返回 nil。我想知道这是否正常/正确?如果不是,我该如何解决?
// A small (WIDTH_MAX:100) window in the center of the screen. If it matters
const CGSize screenSize = [[UIScreen mainScreen] bounds].size;
const CGRect rect = CGRectMake(((int)(screenSize.width - WIDTH_MAX)*0.5),
((int)(screenSize.height - WIDTH_MAX)*0.5), WIDTH_MAX, WIDTH_MAX);
overlayWindow = [[CustomWindow alloc] initWithFrame:rect];
overlayWindow.windowLevel = UIWindowLevelStatusBar; //1000.0
overlayWindow.hidden = NO; // I don't need it to be the key (no makeKeyAndVisible)
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// Find the front most window (with the highest window level) and
// call this method on that window. It should will make the event be
// forwarded to it
// Situation1: This method is called twice (or even more, it depend
// on the number of windows the app has) per event: Why? Is this the
// *normal* behaviour?
NSLog(@" ");
NSLog(@"Point: %@ Event: %p\n", NSStringFromCGPoint(point), event);
UIView *view = nil;
if (CGRectContainsPoint(self.bounds, point)) {
NSLog(@"inside window\n");
NSArray *wins = [[UIApplication sharedApplication] windows];
__block UIWindow *frontMostWin = nil;
[wins enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"win: %@\n", obj);
if ([obj windowLevel] >= [frontMostWin windowLevel] && obj != self) {
frontMostWin = obj;
}
}];
NSLog(@"frontMostWindow:%@\n finding a new view ...\n", frontMostWin);
CGPoint p = [frontMostWindow convertPoint:point fromWindow:self];
view = [frontMostWindow hitTest:p withEvent:event];
// Situation2: sometimes view is nil here, Is that correct?
}
NSLog(@"resultView: %@\n", view);
return view;
}
编辑:
我也注意到了
如果
hitTest:withEvent:
总是返回nil
它也可以。只有当我打电话时overlayWindow.hidden = NO;
如果我打电话
[overlayWindow makeKeyAndVisible]
回来并不总是有效nil
。hitTest:withEvent:
看起来关键窗口需要正确实施命中测试方法?
我在这里错过了关于事件转发的一些东西吗?