CAShapeLayer hitTest touch

ale*_*lex 1 iphone core-graphics quartz-graphics uikit cashapelayer

我无法理解为什么CAShapeLayer不响应hitTest

这个功能总是去//触摸外面

如何检测CAShapeLayer上的触摸?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   

    currentPoint = [[touches anyObject] locationInView:self];

    for (CAShapeLayer *layer in self.layer.sublayers) {    

        if(layer == shapeLayer) {

            if([layer hitTest:currentPoint])
            {
                // touche is on the layer
            }
            else {
                // touche is outside
            }

        }

    }       

}

ale*_*lex 6

在敲了两天之后,我能够生成这个奇怪的代码,看起来它正在工作!

目标是打测试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  
    }   

}