I have a custom window (which should be on top of everything, including the keyboard) to show the superimposed thing, something like the overlay that you see when you press the volume up / down buttons on the device.
So, I made my own OverlayWindow window, while everything is working fine, and windows in the back usually receive their events. However, hitTest:withEvent: is called several times, and sometimes even returns nil. I wonder if this is normal? If not, how can I fix this?
- (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; }
EDIT:
I also noticed that
If hitTest:withEvent: always returns nil , it works too. This only happens when I call overlayWindow.hidden = NO;
if I call [overlayWindow makeKeyAndVisible] returning nil in hitTest:withEvent: does not always work. It looks like the key window requires the correct implementation of the hit test method?
Am I missing something about event forwarding here?
source share