我总共有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];发布于 2015-09-20 19:41:43
您的代码没有任何意义:
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时保持对它的引用,并首先将其放入接口中。
[self.view.layer addSublayer:lineShape];
// now store a reference to this layer in an array that you can use later!https://stackoverflow.com/questions/32683480
复制相似问题