• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 获取与线IOS相交的所有 View

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:36:44 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap