【发布时间】:2012-07-13 15:07:23
【问题描述】:
我正在自定义UIView 中创建多个自定义UIView's。自定义子视图的创建是好的。它们看起来像这样:
draw 方法很简单:
[[UIColor brownColor] set];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx,
5.0f);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 0.0f, 0.0f);
CGContextAddLineToPoint(ctx, 100.0f, 0.0);
CGContextAddLineToPoint(ctx, 130.0f, 25.0f);
CGContextAddLineToPoint(ctx, 100.0f, 50.0f);
CGContextAddLineToPoint(ctx, 0.0f, 50.0f);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
[super drawRect:rect];
将其添加到超级视图中也很简单:
ITContextFigure *view = [[ITContextFigure alloc] initWithFrame:CGRectMake(location.x, location.y, 135.0f, 50.0f)];
[view setBackgroundColor:[UIColor yellowColor]];
[self addSubview:view];
所以我的问题是:
1) 如何检测一个与另一个重叠的时间?
我看到了这个解决方案:
if (CGRectContainsRect([myImageView1 frame], [myImageView2 frame])) {
NSLog(@"Overlaped, it's working!");
}
但如果我有多个UIViews,在super view 上执行for 并检查每个子视图对我来说似乎不是一个好的解决方案。
2) 在这种情况下,可以做什么?
我的主要目标是检测何时发生这种情况:
更新 1.0
将尝试here 显示的内容,因为没有更优雅的方法。如果我能够实现它,我会将代码发布到 Github 上,如果有人需要的话。
【问题讨论】:
-
我想你可能想搜索算法来检测相交的多边形(这涉及暴力每对线段和线段相交)。
-
我猜你在 2) 中的情况是边界矩形重叠,但视图的主要内容没有?
-
@nhahtdh 并不总是蛮力,您可以使用分离轴定理来检测任意(凸)多边形上的交点
-
@DanF 是的。 nhahtdh 这就是我要避免的。
-
@DanF:我同意这可能会有所帮助,但在这种情况下它会比蛮力更好吗?
标签: ios uiview overlap subviews