How to redraw a CAShapeLayer when modifying his path?
Asked Answered
I

1

6

I'm trying to add a subpath to my CAShapeLayer's path, but the changes doesn't show unless I first assing nil to the path and then I reassign myPath to the CAShapeLayer. I've tried with setNeedsRedisplay but it doesn't works.

This is the code in the LoadView where myPath and shapeLayer are properties:

// The CGMutablePathRef
CGPathMoveToPoint(myPath, nil, self.view.center.x, self.view.center.y);
CGPathAddLineToPoint(myPath, nil, self.view.center.x + 100.0, self.view.center.y - 100.0);

// The CAShapeLayer
shapeLayer = [CAShapeLayer layer];
shapeLayer.path = myPath;
shapeLayer.strokeColor = [UIColor greenColor].CGColor;
shapeLayer.lineWidth = 2.0;
[self.view.layer addSublayer:shapeLayer];

And this is, for example, where I perform the changes to the path. I simply add a new subpath to myPath:

- (void)handleOneFingerSingleTapGesture:(UIGestureRecognizer *)sender
{
    // I add a new subpath to 'myPath'
    CGPathMoveToPoint(myPath, nil, self.view.center.x, self.view.center.y);
    CGPathAddLineToPoint(myPath, nil, self.view.center.x + 100.0, self.view.center.y + 100.0);

    // I must do this to show the changes
    shapeLayer.path = nil; 
    shapeLayer.path = myPath;
}

Anyone knows how to deal with this?. I'm using iOS 5.0.

Impunity answered 11/8, 2012 at 20:13 Comment(0)
C
0

You have to call [shapeLayer didChangeValueForKey:@"path"] to get the layer to re-render itself.

Cunning answered 3/8, 2013 at 0:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.