문제

Alright. I am new to objective-c, and I am trying to detect two views overlapping. I originally used CGRectContainsRect (which worked perfectly) but noticed that this calls true even if the views barely overlap. 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.

Making this change is causing me some issues. Here is my whole if statement (though I have isolated the issue to the CGRectContainsPoint part:

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]. Now I am attempting to use the overlapV view and check if it contains the center of view 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.

What really puzzles me is that there is no error shown with this if statement line. 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). 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).

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).

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). For instance, in my most recent test, the breakpoint highlighted line NSLog(@"start"); and said Thread 1: breakpoint 2.1, but no output was printed to the console like a typical error message.

Big Update: Now to my astonishment, changing the CGRectContainsPoint back to the original, working, CGRectContainsRece([overlapV frame], [colorV frame]) now doesn't work. However, the difference is that the program doesn't break when it should evaluate to True; it just keeps running like the if statement never should have been called.

도움이 되었습니까?

해결책

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:

enter image description here

Or you can click the "continue program execution" button at the bottom:

enter image description here

ORIGINAL ANSWER:
Just to make debugging easier, I would suggest re-writing your if statement like this:

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. 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top