gpt4 book ai didi

iphone - 如何检查用户是否在 CGPath 附近点击?

转载 作者:行者123 更新时间:2023-12-03 18:29:54 31 4
gpt4 key购买 nike

场景:

我有一组CGPath。它们大多只是线条(即不是闭合形状)。它们通过 UIView 的绘制方法绘制在屏幕上。

如何检查用户是否在其中一条路径附近点击?

这是我所做的工作:

UIGraphincsBeginImageContext(CGPathGetBoundingBox(path));
CGContextRef g = UIGraphicsGetCurrentContext();
CGContextAddPath(g,path);
CGContextSetLineWidth(g,15);
CGContextReplacePathWithStrokedPath(g);
CGPath clickArea = CGContextCopyPath(g); //Not documented
UIGraphicsEndImageContext();

所以我正在做的是创建一个图像上下文,因为它具有我需要的功能。然后,我将路径添加到上下文中,并将线宽设置为 15。此时抚摸路径将创建点击区域,我可以在其中检查以查找点击。因此,我通过告诉上下文将路径转换为描边路径,然后将该路径复制回另一个 CGPath 来获得该描边路径。稍后我可以检查:

if (CGPathContainsPoint(clickArea,NULL,point,NO)) { ...

这一切都运行得很好,但是 CGContextCopyPath 由于没有文档记录,由于显而易见的原因,使用它似乎是一个坏主意。仅出于此目的创建 CGContext 也有一定的笨拙性。

那么,有人有什么想法吗?如何检查用户是否在 CGPath 上的任何区域附近(在本例中为 15 像素内)点击?

最佳答案

在 iOS 5.0 及更高版本中,可以使用 CGPathCreateCopyByStrokingPath 更简单地完成此操作:

CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(path, NULL, 15,
kCGLineCapRound, kCGLineJoinRound, 1);
BOOL pointIsNearPath = CGPathContainsPoint(strokedPath, NULL, point, NO);
CGPathRelease(strokedPath);

if (pointIsNearPath) ...

关于iphone - 如何检查用户是否在 CGPath 附近点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1143704/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com