gpt4 book ai didi

ios - 无法删除 CAShapeLayer

转载 作者:行者123 更新时间:2023-11-29 01:38:40 25 4
gpt4 key购买 nike

我在屏幕上总共画了 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(即您的对象)一遍又一遍地添加到图层数组。它永远不会界面中,它永远不会拥有 super 层,因此从 super 层中删除它不会起任何作用。

此外,这一行毫无意义:

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

subview 永远不会是CAShapeLayer。 subview 是 UIView。它不是任何类型的层(尽管它一个层)。

想要的是查找界面中已有的CAShapeLayers。当然,一个简单的方法是在创建每个 CAShapeLayer 时保留对每个 CAShapeLayer 的引用,并将其首先放入界面中:

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

关于ios - 无法删除 CAShapeLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32683480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com