iphone – [CustomWindow hitTest:withEvent:]实现转发事件

我有一个自定义窗口(应该在所有东西的顶部,包括键盘)来显示重叠的东西,比如在按下设备中的音量增大/减小按钮时看到的叠加.

所以我做了一个自定义窗口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时;
>如果我在hitTest中调用[overlayWindow makeKeyAndVisible]返回nil:withEvent:并不总是有效.它看起来像一个关键窗口需要正确实现命中测试方法?

我在这里错过了关于事件转发的内容吗?

最佳答案 frontMostWindow是指frontMostWin吗?

看起来即使我们只使用一个UIWindow,hitTest:withEvent:也会在其上执行至少2次.所以,我想这是正常的.

您可以在

view = [frontMostWindow hitTest:p withEvent:event];

由于以下原因:

> frontMostWindow本身为null(例如,如果您只有一个窗口)
> p是ouside frontMostWindow边界(例如,当frontMostWindow是键盘而你的触摸在其他地方时)
> frontMostWindow将属性userInteractionEnabled设置为NO;

点赞