ContainsPoint not working with CAShapeLayer?

I have two CAShapeLayers that overlay the main layer of the UIView. CAShapeLayers have complex shapes, and I need to know if a point within the boundaries of the form has been touched. In addition, I need to know which form has been affected.

I am trying to contain containsPoint but nothing works.

+4
source share
1 answer

After I hit my head for two days, I was able to create this strange code and it seems to work!

The goal was to push the CAShapeLayer test. CAShapeLayer moves around the screen, so the form is not in a constant place. Hittesting CGPath currentPoint is not simple.

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  
     }   

 }
+7
source

Source: https://habr.com/ru/post/1339539/


All Articles