ios - 使用CAShapeLayer的HitTest无法正常工作

标签 ios path collision-detection hittest cashapelayer

我使用以下代码检查玩家的点是否在圆形区域中:

if ([circle.presentationLayer hitTest:player.position])
{
    NSLog(@"hit");
}

我的圈子是CAShapeLayer:
CAShapeLayer *circle = [CAShapeLayer layer];
CGFloat radius = 50; 
[circle setMasksToBounds:YES];
[circle setBackgroundColor:[UIColor redColor].CGColor];
[circle setCornerRadius:radius1];
[circle setBounds:CGRectMake(0.0f, 0.0f, radius *2, radius *2)];
[self.view.layer addSublayer:circle];

这样,碰撞检测效果很好。

现在,我不想用圆形图层来测试玩家的位置,而是要沿着自定义路径绘制CAShapeLayer:
CAShapeLayer *customLayer = [CAShapeLayer layer];
customLayer.path = customPath.CGPath;
customLayer.fillColor = [UIColor yellowColor].CGColor;
customLayer.shouldRasterize = YES;
customLayer.opacity = 0.2;
[self.view.layer addSublayer: customLayer];

当我想使用自定义图层对玩家的位置进行击打测试时,击打测试将不再起作用。

我怎么解决这个问题?

最佳答案

您是否正在将位置转换为正确的相对位置?

例如:

CGPoint layerPoint = [[dynamicView layer] convertPoint:touchLocation toLayer:sublayer];

另外,也许您需要CGPathContainsPoint:
if(CGPathContainsPoint(shapeLayer.path, 0, layerPoint, YES))
{

关于ios - 使用CAShapeLayer的HitTest无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17008933/

相关文章:

ios - *** 由于 UITableView 中未捕获的异常 'NSRangeException' 而终止应用程序

查找图中哈密顿路径数的算法

algorithm - 如何检测多段线(多角度线)上圆的碰撞?

python - 检测鼠标是否越过一行python

ios - 如何创建对象并在Objective-C中使用它?

ios - 找不到选项 '-L/Users/..../TestFlightSDK1.1' 的目录

node.js - 修改 $PATH 变量

android - 在android中绘制单点触摸运动路径

3d - 我如何找到光线在 3d 网格中相交的所有立方体?

ios - UIPageViewController setViewControllers