Question

I'm trying to modify a the path of my CAShapeLayer and then redraw it using setNeedsDisplay,

But for some reason it wont redraw. The only way i can make it to be redrawn is calling viewDidLoad which i am sure is not the correct way.

Here is the code for initialisation:

self.shape.lineWidth = 2.0;
[self.shape setFillColor:[[UIColor clearColor] CGColor]];
self.shape.strokeColor = [[UIColor blackColor] CGColor];
[self.shape setPath:[self.shapePath CGPath]];
self.shape.fillRule = kCAFillModeRemoved;
[self.cuttingView.layer addSublayer:self.shape];

And the gesture recogniser for the drag gesture:

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    static CGPoint lastLocation;
    CGPoint location = [gesture locationInView:self.cuttingView];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        if (![self.shapePath containsPoint:location] ) {
            gesture.enabled = NO;
        } else {
            lastLocation = location;
        }
    }

    if (gesture.state == UIGestureRecognizerStateChanged) {
        CGAffineTransform translation = CGAffineTransformMakeTranslation((location.x - lastLocation.x), (location.y - lastLocation.y));
        self.shapePath.CGPath = CGPathCreateCopyByTransformingPath(self.shapePath.CGPath, &translation);
        [self.shape setNeedsDisplay];
    }
    gesture.enabled = YES;
} 

I am printing the locations, and they do change, but the screen shows nothing.

How could this be fixed?

Thanks!

Was it helpful?

Solution

You are changing self.shapePath.CGPath, but you are not changing the path in the CAShapeLayer.

Instead of [self.shape setNeedsDisplay], do this again:

[self.shape setPath:[self.shapePath CGPath]];

OTHER TIPS

you don't assign the path to the layer. I'd remove the shapePath variable as it has no use (AFAICS)

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