Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAShapeLayer not updating when changing path

I'm using a CAShapeLayer. I'm trying to make it so that its path changes whenever a mouse event occurs. For example, when a mouse down event occurs, a path is appended to a CGMutableRefPath and then then the shape layer's path is set to it.

The problem: When I set the CAShapeLayer path with a new path, the display is not updated with a new path unless I do . How do I tell the system that the shape layer's path has changed and to redraw itself?

Note: When I create a copy of the mutable path and pass it along, it works, but doing a copy every time is not performant and causes my computer to slow down when there are lots of paths.

CGMutablePathRef paths = CGPathCreateMutable();
CAShapeLayer *shapeLayer = [CAShapeLayer layer];

- (void)mouseDown:(NSEvent *)event {
    CGPathAddEllipseInRect(paths, nil, rect);
    self.shapeLayer.path = paths; // Works once, but subsequent calls do nothing
    self.shapeLayer.path = CGPathCreateCopy(paths); // Works, but super slow
}
like image 922
user2647249 Avatar asked Jan 13 '23 17:01

user2647249


1 Answers

Call [self.shapeLayer didChangeValueForKey:@"path"] to force the layer to redraw itself.

like image 165
user2647249 Avatar answered Feb 15 '23 05:02

user2647249