我有两个CAShapeLayers insde UIView的主要层。 CAShapeLayers具有复杂的形状,我需要知道在形状边界内是否触摸了一个点。另外,我需要知道哪个形状被触及了。
我正在尝试使用containsPoint,但没有任何效果。
答案 0 :(得分:7)
在我的头撞了两天后,我能够制作出这个奇怪的代码,看起来它正在工作!
目标是打击测试CAShapeLayer。 CAShapeLayer正在屏幕上移动,因此形状不在恒定的位置。 Hittesting CGPath currentPoint并不简单。
随意添加任何输入......
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint p = [[touches anyObject] locationInView:self]; CGAffineTransform transf = CGAffineTransformMakeTranslation(-shapeLayer.position.x, -shapeLayer.position.y); if(CGPathContainsPoint(shapeLayer.path, &transf, p, NO)){ // the touch is inside the shape } }