Chris.Stover
Chris.Stover

Reputation: 514

Unable to remove CAShapeLayer

I have a total of 21 lines drawn across the screen for a measurement tool. The lines are drawn as a layer on UIViewController.view. I am attempting to remove the lines as follows. The NSLog statement confirms that I am getting all 21 CAShapeLayers that I am looking for.

CAShapeLayer* layer = [[CAShapeLayer alloc]init];
    for (UIView *subview in viewsToRemove){
        if([subview isKindOfClass:[CAShapeLayer class]]){
            count++;
            NSLog(@"%d: %@", count, subview);
            [layerArray addObject:layer];
        }
    }
    for(CAShapeLayer *l in layerArray){
        [l removeFromSuperlayer];
    }

Any help getting these lines removed would be greatly appreciated.

I don't think it's necessary but if you want to see the code to draw the lines here it is:

for(int i = 0; i < numberOfColumns; i++){
    CAShapeLayer *lineShape = nil;
    CGMutablePathRef linePath = nil;
    linePath = CGPathCreateMutable();
    lineShape = [CAShapeLayer layer];
    if(i == 0 || i == 20 || i == 10 || i == 3 || i == 17)
        lineShape.lineWidth = 4.0f;
    else
        lineShape.lineWidth = 2.0f;
    lineShape.lineCap = kCALineCapRound;
    if( i == 0 || i == 20)
        lineShape.strokeColor = [[UIColor whiteColor]CGColor];
    else if(i == 3 || i == 17)
        lineShape.strokeColor = [[UIColor redColor]CGColor];
    else if (i == 10)
        lineShape.strokeColor = [[UIColor blackColor]CGColor];
    else
        lineShape.strokeColor = [[UIColor grayColor]CGColor];

    x += xIncrement;  y = 5;


    int toY = screenHeight - self.toolBar.frame.size.height - 10;
    CGPathMoveToPoint(linePath, NULL, x, y);
    CGPathAddLineToPoint(linePath, NULL, x, toY);
    lineShape.path = linePath;
    CGPathRelease(linePath);
    [self.view.layer addSublayer:lineShape];

Upvotes: 0

Views: 540

Answers (1)

matt
matt

Reputation: 535222

Your code makes no sense:

CAShapeLayer* layer = [[CAShapeLayer alloc]init];
for (UIView *subview in viewsToRemove){
    if([subview isKindOfClass:[CAShapeLayer class]]){
        count++;
        NSLog(@"%d: %@", count, subview);
        [layerArray addObject:layer];
    }
}
for(CAShapeLayer *l in layerArray){
    [l removeFromSuperlayer];
}

You are creating a completely new CAShapeLayer and then adding that same CAShapeLayer (namely your layer object) over and over to the layerArray. It is never in the interface, it never has a superlayer, and so removing it from its superlayer does nothing.

Moreover, this line is pointless:

if([subview isKindOfClass:[CAShapeLayer class]]){

A subview will never be a CAShapeLayer. A subview is a UIView. It is not a layer of any kind (though it has a layer).

What you want to is look for the CAShapeLayers that are already in the interface. Of course, an easy way to do this would have been to keep a reference to each CAShapeLayer at the time you created it and put it into the interface in the first place:

[self.view.layer addSublayer:lineShape];
// now store a reference to this layer in an array that you can use later!

Upvotes: 1

Related Questions