2

我有一个小(30X30 大小)UIView 的网格,我通过使用以下代码在屏幕上点击两个点作为起点和终点在它们上面画一条线:

CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {244.0f/255.0f, 226.0f/255.0f, 119.0f/255.0f, 0.8};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineWidth(context, 20.0);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);

点击屏幕上的两个点并画一条线可以正常工作,但是如何让所有视图与线相交?我想在 touches end 方法中获得这些视图。

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  CGPoint pt = [[touches anyObject] locationInView:alphabetView];
  UIView *touched = [alphabetView hitTest:pt withEvent:event];
  CGPoint p = touched.center;
  // code here to get view list.
}

任何帮助,将不胜感激。

4

2 回答 2

1

如果满足以下条件,则线段(从 A 点到 B 点)与矩形(视图框架)相交:

  1. 线段与 4 个矩形边中的任何一个相交,
  2. 点 A 和 B 都在矩形内。

如果 (1) 和 (2) 的答案均为“否”,则线段不与矩形相交。

此答案checkLineIntersection中的函数可能有助于检查条件 (1)。

CGRectContainsPoint()可用于检查条件 (2)。

于 2013-04-17T08:38:36.307 回答
0

你可以CGContextPathContainsPoint()用来检查你CGPoint是否在你的行的路径中,它返回一个布尔值。它的参考可以在这里找到

于 2013-04-17T06:19:44.560 回答