Question

I have two CAShapeLayers insde the main layer of UIView. The CAShapeLayers have complex shapes and I need to know if a point was touched within the shape boundaries. Also, I need to know which shape got touched.

I am trying the containsPoint, but nothing works.

Was it helpful?

Solution

After banging my head for two days I was able to produce this bizarre code and looks like it is working!

The goal was to hit test CAShapeLayer. The CAShapeLayer is moving on the screen, so the shape is not in constant place. Hittesting the CGPath currentPoint is not straightforward.

Feel free to add any input...

-(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  
    }   

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top