4

我有一个自定义窗口(应该在所有内容之上,包括键盘)来显示叠加层,就像您在设备中按下音量增大/减小按钮时看到的叠加层。

所以我做了一个自定义窗口 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;
}

编辑:

我也注意到了

  1. 如果hitTest:withEvent:总是返回nil它也可以。只有当我打电话时overlayWindow.hidden = NO;

  2. 如果我打电话[overlayWindow makeKeyAndVisible]回来并不总是有效nilhitTest:withEvent:看起来关键窗口需要正确实施命中测试方法?

我在这里错过了关于事件转发的一些东西吗?

4

3 回答 3

2

frontMostWindow 是指 frontMostWin 吗?

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

您可以在收到 null

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

由于以下原因:

  • frontMostWindow 本身为空(例如,如果您只有一个窗口)
  • p 在 frontMostWindow 边界之外(例如,当 frontMostWindow 是键盘并且您的触摸在其他地方时)
  • frontMostWindow 的属性 userInteractionEnabled 设置为 NO;
于 2012-06-28T14:49:34.287 回答
0

hitTest:withEvent: 被多次调用是正常的。这可能是因为您只在覆盖的窗口上显示 UILabel 或 UIImageView ,因此会自动转发触摸。

但是我认为您并不真的需要另一个 OverlayWindow,而是您可以考虑在 keyWindow 顶部使用 UIView。这应该使您的应用程序更清洁...

于 2012-06-25T08:13:04.470 回答
0

我确实遇到了同样的问题。

我试图根据您的帖子解决它,并找到了另一个解决方案。

此代码在顶部覆盖的 uiwindow 中实现。

当区域没有视图时,此代码将通过下窗口传递事件。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSLog(@" ");
    NSLog(@"Point: %@ Event: %p\n", NSStringFromCGPoint(point), event);
    UIView *view = nil;
    
    UIView *resultView = [super hitTest:point withEvent:event];
    
    if (resultView == self) {
        NSLog(@"touched in transparent window !!");
        return nil;
    }

    NSLog(@"touched in view!!");
    return resultView;
 }

毕竟,谢谢。你的帖子很有帮助。

于 2017-02-19T04:46:51.297 回答