无法删除 CAShapeLayer
Unable to remove CAShapeLayer
我在屏幕上总共画了 21 条线作为测量工具。这些线条在 UIViewController.view 上绘制为一层。我正在尝试按如下方式删除这些行。 NSLog 声明确认我正在获取我正在寻找的所有 21 个 CAShapeLayers。
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];
}
如能帮助我们删除这些行,我们将不胜感激。
我认为没有必要,但如果您想查看此处绘制线条的代码:
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];
你的代码没有意义:
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];
}
您正在创建一个全新的 CAShapeLayer,然后不断添加同一个CAShapeLayer(即您的layer
对象)转到 layerArray
。它永远不会 在 界面中,它永远不会 有 超级层,因此从它的超级层中删除它没有任何作用。
此外,这条线毫无意义:
if([subview isKindOfClass:[CAShapeLayer class]]){
子视图永远不会成为 CAShapeLayer。子视图是 UIView。它不是任何类型的图层(尽管它 有 图层)。
您想要的是寻找已经在界面中的CAShapeLayers。当然,一种简单的方法是在创建每个 CAShapeLayer 时保留对每个 CAShapeLayer 的引用,并首先将其放入界面:
[self.view.layer addSublayer:lineShape];
// now store a reference to this layer in an array that you can use later!
我在屏幕上总共画了 21 条线作为测量工具。这些线条在 UIViewController.view 上绘制为一层。我正在尝试按如下方式删除这些行。 NSLog 声明确认我正在获取我正在寻找的所有 21 个 CAShapeLayers。
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];
}
如能帮助我们删除这些行,我们将不胜感激。
我认为没有必要,但如果您想查看此处绘制线条的代码:
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];
你的代码没有意义:
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];
}
您正在创建一个全新的 CAShapeLayer,然后不断添加同一个CAShapeLayer(即您的layer
对象)转到 layerArray
。它永远不会 在 界面中,它永远不会 有 超级层,因此从它的超级层中删除它没有任何作用。
此外,这条线毫无意义:
if([subview isKindOfClass:[CAShapeLayer class]]){
子视图永远不会成为 CAShapeLayer。子视图是 UIView。它不是任何类型的图层(尽管它 有 图层)。
您想要的是寻找已经在界面中的CAShapeLayers。当然,一种简单的方法是在创建每个 CAShapeLayer 时保留对每个 CAShapeLayer 的引用,并首先将其放入界面:
[self.view.layer addSublayer:lineShape];
// now store a reference to this layer in an array that you can use later!