问题描述
我在屏幕上总共绘制了 21 条线用于测量工具.线条在 UIViewController.view 上绘制为一个图层.我正在尝试按如下方式删除这些行.NSLog 语句确认我得到了我正在寻找的所有 21 个 CAShapeLayers.
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];
推荐答案
你的代码毫无意义:
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
.它永远不会在界面中,它永远不会有一个超层,因此将它从它的超层中移除没有任何作用.
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]]){
子视图永远将成为 CAShapeLayer.一个子视图是一个 UIView.它不是任何类型的层(尽管它有一层).
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).
您想要的是寻找已经在界面中的 CAShapeLayers.当然,一个简单的方法是在创建它时保留对每个 CAShapeLayer 的引用,并首先将其放入界面:
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!
这篇关于无法删除 CAShapeLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!