0

I am drawing line over 5 custom uiviews (Like UITableView rows) from one position to another (X,Y axis) using CAShapeLayer.

My issue is that I want to know that what view the CAShapeLayer (line in my case) is currently in. Is there any CGIntersect for CALayer and CGRect etc? I am trying to create graph using core animation instead of any chart API.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
york
  • 149
  • 1
  • 11

1 Answers1

1

You could check in the shape layer intersects a specific layer by checking intersection of the bounding box of the path agains the frame of the other layer.

BOOL doesIntersect = CGRectIntersectsRect(CGPathGetBoundingBox(path), layer.frame);
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • Thanks for your response david. But this line of code is crashing my application. I have a CAShapeLayer that i want to match with some uiview frames, whether the CAShapeLayer occur on that particular frame or not. – york May 21 '13 at 11:52
  • 1
    @york You got that the path is not the shape, right? It's the path. `CGPathRef path = shapeLayer.path;` – David Rönnqvist May 21 '13 at 11:55
  • This is the code i am using CGMutablePathRef pointPath = CGPathCreateMutable(); CGPathMoveToPoint(pointPath, NULL, 20, 25); CGPathAddLineToPoint(pointPath, NULL, 30, 35); CGPathAddLineToPoint(pointPath, NULL, 40,45); CGPathAddLineToPoint(pointPath, NULL, 80,115); pathAnimation.path = pointPath; myLayer = [[CAShapeLayer alloc] init]; myLayer.path = pointPath; myLayer.strokeColor = [[UIColor greenColor] CGColor]; myLayer.lineWidth = 4.0; myLayer.lineJoin = kCALineJoinBevel; [self.layer addSublayer:myLayer]; //Add layer in custom view – york May 21 '13 at 12:04
  • yes, it is a path not a shape. Now i want to check whether myLayer intersects with my custom views or not. – york May 21 '13 at 12:08
  • what error is it crashing with and what what are you passing for the intersection? – David Rönnqvist May 21 '13 at 12:10
  • I am comparing like this: if (CGRectIntersectsRect(CGPathGetBoundingBox((__bridge CGPathRef)(myLayer)), view.frame)) – york May 21 '13 at 12:13
  • 1
    `(__bridge CGPathRef)(myLayer)` is a layer, not a path... you should be getting the path (as I said above). That is why I called the variable "path" – David Rönnqvist May 21 '13 at 12:17
  • Thanks david, i got the issue and able to resolve it now. It is now intersecting my my frames. thanks – york May 21 '13 at 12:36