문제

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!

도움이 되었습니까?

해결책

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]];

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top