我使用以下代码检查玩家的点是否在圆形区域中:
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/