简体   繁体   English

确定两个视图之间的重叠时,CGRectContainsPoint为什么会导致错误?

[英]Why does CGRectContainsPoint cause error when determining overlap between two views?

Alright. 好的。 I am new to objective-c, and I am trying to detect two views overlapping. 我是Objective-c的新手,我试图检测两个重叠的视图。 I originally used CGRectContainsRect (which worked perfectly) but noticed that this calls true even if the views barely overlap. 我最初使用CGRectContainsRect (效果很好),但注意到即使视图几乎不重叠,这也称为true。 However, because the views are small, I only want the condition to be true if they heavily overlap. 但是,由于视图很小,因此仅当条件严重重叠时才希望条件成立。 So, I am trying to use CGRectContainsPoint instead, where the point I check is the center of the second view. 因此,我尝试改用CGRectContainsPoint ,我检查的点是第二个视图的中心。

Making this change is causing me some issues. 进行此更改会引起一些问题。 Here is my whole if statement (though I have isolated the issue to the CGRectContainsPoint part: 这是我的整个if语句(尽管我已将问题隔离到CGRectContainsPoint部分:

NSDictionary *dictInQ = masterDict[n];
UIView *overlapV = dictInQ[@"view"];
NSNumber *overlapB = dictInQ[@"hiddenBool"];
//if it's not itself and they intersect and they are in the same column and both views are not hidden
if (overlapV != colorV && CGRectContainsPoint([overlapV frame], CGPointMake(colorV.center.x, colorV.center.y)) && startLocation == overlapV.frame.origin.x && [colorB intValue]!=1 && [overlapB intValue]!=1)

Originally when I used CGRectContainsRect I used [overlapV frame] and [colorV frame]. 最初,当我使用CGRectContainsRect我使用的是[overlapV frame][colorV frame]. Now I am attempting to use the overlapV view and check if it contains the center of view colorV. 现在,我尝试使用overlapV视图,并检查它是否包含视图中心colorV. I have tried replacing CGPointMake(colorV.center.x, colorV.center.y) with colorV.center to no avail, and I have read documentation on CGRectContainsPoint and CGPointMake and examined similar questions. 我曾尝试更换CGPointMake(colorV.center.x, colorV.center.y)colorV.center无济于事,我已经在CGRectContainsPoint和CGPointMake阅读文档,并检查了类似的问题。

What really puzzles me is that there is no error shown with this if statement line. 真正令我困惑的是,此if语句行没有显示任何错误。 It compiles fine, and when the CGRectContainsPoint should be evaluating true from the simulator, Xcode's error points to the first line executed in the if statement (though this line isn't what's causing problems either; I tried commenting it out and it just raises the error at the next line instead). 它编译良好,并且当CGRectContainsPoint应该从模拟器评估为true时,Xcode的错误指向if语句中执行的第一行(尽管这行也不是引起问题的原因;我尝试将其注释掉,这只会引发下一行错误)。 Any thoughts on what could be causing this? 有什么想法可能导致这种情况?

Update : here is a screenshot of the views when overlapping enough to cause the CGRectContainsPoint to correctly evaluate to true (the trouble is that it also causes an error on the proceeding line). 更新 :这是视图的屏幕快照,当视图重叠足够导致CGRectContainsPoint正确评估为true时(问题在于它还会在后续行中引起错误)。

更新:这是视图的屏幕快照,当视图重叠足够导致CGRectContainsPoint正确评估为true时(问题在于它还会在后续行中引起错误)。

Update : To clarify, the breakpoint always happens on whatever line is called just after the CGRectContainsPoint passes (regardless of what I change the line to be). 更新 :为明确CGRectContainsPoint ,断点总是在CGRectContainsPoint通过之后的任何行上发生(无论我将行更改为什么)。 For instance, in my most recent test, the breakpoint highlighted line NSLog(@"start"); 例如,在我最近的测试中,断点突出显示了行NSLog(@"start"); and said Thread 1: breakpoint 2.1, but no output was printed to the console like a typical error message. 并说“ Thread 1: breakpoint 2.1,但没有输出像典型的错误消息一样输出到控制台。

Big Update : Now to my astonishment, changing the CGRectContainsPoint back to the original, working, CGRectContainsRece([overlapV frame], [colorV frame]) now doesn't work. 大更新 :现在让我惊讶的是,将CGRectContainsPoint更改回原始的,有效的CGRectContainsRece([overlapV frame], [colorV frame])现在不起作用。 However, the difference is that the program doesn't break when it should evaluate to True ; 但是,不同之处在于该程序在应评估为True时不会中断。 it just keeps running like the if statement never should have been called. 它只是一直运行,就像永远不应该调用if语句一样。

EDIT 编辑
I think that I missed the main point of your question earlier. 我认为我错过了您的问题的重点。 A breakpoint is not an error, but simply a way to pause the execution of the program so that you can look at memory and step through the program line by line. 断点不是错误,而只是暂停程序执行的一种方法,因此您可以查看内存并逐行浏览程序。 To get rid of it, simply click the breakpoint symbol to the left of the line of code that has it: 要摆脱它,只需单击包含它的代码行左侧的断点符号:

在此处输入图片说明

Or you can click the "continue program execution" button at the bottom: 或者,您可以单击底部的“继续执行程序”按钮:

在此处输入图片说明

ORIGINAL ANSWER: 原始答案:
Just to make debugging easier, I would suggest re-writing your if statement like this: 为了使调试更容易,我建议像这样重写您的if语句:

if (overlapV != colorV)
{
    // CGPointMake is not required here because a view's center is already a CGPoint.
    // Note that it won't cause a problem, it just isn't needed.
    if (CGRectContainsPoint([overlapV frame], colorV.center))
    {
        if (startLocation == overlapV.frame.origin.x)
        {
            if ([colorB intValue] != 1)
            {
                if ([overlapB intValue] != 1)
                {
                    // All are true
                }
            }
        }
    }
}

Now, you can step through one line at a time and see for sure which line is the problem. 现在,您可以一次浏览一行,并确定是哪一行是问题所在。 The CGRectContainsPoint looks right though. CGRectContainsPoint看起来CGRectContainsPoint If it turns out to actually be the problem, please post a picture of what the views look like when this should be returning true. 如果事实确实是问题所在,请发布一张图片,说明该图片应返回true时的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM