我使用下面的代码来检查玩家的点是否在圆形区域内:
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];当我想用自定义图层测试玩家的位置时,hittest不再起作用了。
我该如何解决这个问题?
发布于 2013-06-10 00:19:12
您是否将位置转换为正确的相对位置?
例如:
CGPoint layerPoint = [[dynamicView layer] convertPoint:touchLocation toLayer:sublayer];另外,也许你需要CGPathContainsPoint
if(CGPathContainsPoint(shapeLayer.path, 0, layerPoint, YES))
{发布于 2017-02-10 06:47:07
您指定的是圆的边界/帧,但不是bezier路径的边界/帧。我不知道这一点,直到我自己偶然发现了它,但是当你创建一个形状并为CAShapeLayer()分配一个路径时,你必须分配CALayer的框架(或者单独的位置和边界),因为根据苹果的文档,CALayer()的框架默认是一个CGZeroRect,当你设置路径时不会自动更新。hitTest()基于position+bounds (或帧),因此失败。文档中写道:
/*返回包含点'p‘的层的最远的后代。*同级按从上到下的顺序进行搜索。'p‘被定义为*接收器最近的祖先的坐标空间中的*不是CATransformLayer (变换层没有2D *坐标空间,可以在其中指定点)。*/
可空*)hitTest:(CGPoint)p; (
如果层的边界包含点'p‘,则/*返回true。*/
一个快速的单元测试显示了这一点(扩展CGPath很有用,包含在后面):
func testShapeLayer() {
let layer = CAShapeLayer()
let bezier = NSBezierPath(rect: NSMakeRect(0,0,10,10))
layer.path = bezier.CGPath
// remove the following line to FAIL the test
layer.frame = NSMakeRect(0,0,10,10)
XCTAssertNotNil(layer.hitTest(CGPoint(x:5,y:5)))
XCTAssertTrue(layer.contains(CGPoint(x:5,y:5)))
}
extension NSBezierPath {
public var CGPath: CGPath {
let path = CGMutablePath()
var points = [CGPoint](repeating: .zero, count: 3)
for i in 0 ..< self.elementCount {
let type = self.element(at: i, associatedPoints: &points)
switch type {
case .moveToBezierPathElement: path.move(to: CGPoint(x: points[0].x, y: points[0].y) )
case .lineToBezierPathElement: path.addLine(to: CGPoint(x: points[0].x, y: points[0].y) )
case .curveToBezierPathElement: path.addCurve( to: CGPoint(x: points[2].x, y: points[2].y),
control1: CGPoint(x: points[0].x, y: points[0].y),
control2: CGPoint(x: points[1].x, y: points[1].y) )
case .closePathBezierPathElement: path.closeSubpath()
}
}
return path
}}
然而,这可能不是您想要的:您希望将路径用于hitTesting(),而不仅仅是框架。因此,我可能会在您的hitTesting例程中添加一个扩展:如果命中CAShapeLayer(),这意味着它在框架内,那么您可以更具体地使用CGPathContainsPoint()检查,就像前面提到的另一个答案一样。
https://stackoverflow.com/questions/17008933
复制相似问题