OGeek|极客世界-中国程序员成长平台

标题: ios - 获取与线IOS相交的所有 View [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:36
标题: ios - 获取与线IOS相交的所有 View

我有一个小的(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);

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

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

任何帮助将不胜感激。



Best Answer-推荐答案


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

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

如果 (1) 和 (2) 的答案都是 NO,则线段不与 矩形。

函数 checkLineIntersection 来自 this answer可能 有助于检查条件 (1)。

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

关于ios - 获取与线IOS相交的所有 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16052696/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4