xcode CGRectIntersectsRect
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6698085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CGRectIntersectsRect
提问by user842059
Possible Duplicate:
CGRectIntersectsRect Problem
可能的重复:
CGRectIntersectsRect 问题
I am making a app with a maze, I put a ball inside the maze in the interface builder (I put an outlet for it) I have a touchesMoved:
我正在制作一个带有迷宫的应用程序,我在界面构建器的迷宫内放了一个球(我为它放了一个出口)我有一个touchesMoved:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint point;
point = [touch locationInView:self.view];
ball.center = point;
if (CGRectIntersectsRect(ball.frame, maze.frame)) {
//my stuff
}
}
I have two CGRectIntersectsRect if statements, I say, if the ball's frame touches the maze's frame, then // my stuff happens, but for some reason, whenever i try to move the ball, without touching the frame of the maze, // my stuff happens. I dont know why, maybe it is because the ball is IN the maze, probably not because i said if cgrectintersectsrect frame not bounds. so why is this happening?
我有两个 CGRectIntersectsRect if 语句,我说,如果球的框架接触了迷宫的框架,那么 // 我的事情发生了,但出于某种原因,每当我尝试移动球时,而不接触迷宫的框架,// 我的事情发生。我不知道为什么,也许是因为球在迷宫中,也许不是因为我说如果 cgrectintersectsrect 框架没有边界。那么为什么会这样呢?
回答by highlycaffeinated
CGRectIntersectsRect
is going to return true if any part of rect1 lies inside of rect2. So while your ball is completely inside your maze, your intersection test will always be true.
CGRectIntersectsRect
如果 rect1 的任何部分位于 rect2 内,则将返回 true。因此,当您的球完全在迷宫内时,您的交叉测试将始终正确。
回答by hatfinch
If you want to detect when the rect of your ball is touching the edge of the rect of your maze (rather than just anywhere in the maze), you need to test for !CGRectEqualToRect(CGRectIntersection(ball, maze), maze)
in addition to CGRectIntersectsRect(ball, maze)
.
如果您想检测球的矩形何时接触迷宫矩形的边缘(而不是迷宫中的任何地方),则!CGRectEqualToRect(CGRectIntersection(ball, maze), maze)
除了CGRectIntersectsRect(ball, maze)
.