【问题标题】:Why doesn't hitTest return the view attached to the window?为什么 hitTest 不返回附加到窗口的视图?
【发布时间】:2013-02-01 16:41:16
【问题描述】:

有人可以花点时间给我指点吗?

我有一个 UIView 附加到一个 UIWindow,我正在玩点的 hitTesting(原因比我想在这里讨论的要复杂得多)。

给定一个作为窗口子视图附加的视图,我希望 hitTest 会找到该视图,但它似乎没有:

- (void)test_hitTest_shouldFindTheViewAttachedToAWindow {
    UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 210, 520)];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 200, 500)];
    [window addSubview:view];

    // This is ok
    GHAssertEquals([view hitTest:CGPointMake(110, 270) withEvent:nil], view, nil);

    // This fails: why? I would expect it to return the view, but it return null.
    GHAssertEquals([view.window hitTest:CGPointMake(110, 270) withEvent:nil], view, nil);
}

重点是在窗口边界内清除,对吧?为什么找不到视图?

【问题讨论】:

    标签: ios uiview uiwindow hittest


    【解决方案1】:

    UIWindow 默认创建为不可见,这意味着hitTest:withEvent 将忽略它。如果您设置了view.window.hidden = NO,那么hitTest:withEvent 将按照您的预期工作。

    【讨论】:

    • 谢谢,一个明显的解决方案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多