我有一个mapView,它有34个CAShapeLayer来渲染每个省,另有34个CATextLayer来渲染每个省的名称。我将计算每个省的中心。
现在我将这个mapView添加到UIScrollView中,当我缩放mapView时,我想重新绘制CATextLayer以使用另一种字体大小。因此,在缩放之后,我将手动删除所有CATextLayer并重绘它们,如下所示。
然而,我发现,在完成for循环之后,子层中仍然存在一些CATextLayer,确切地说,每次我测试它时,都有18个CATextLayer没有被删除。我从未遇到过这个问题,我会错过什么吗?请帮助,谢谢。
-(void)drawLabelsWithFontSize:(CGFloat)fontSize {
int i = 0;
NSUInteger count = [self.mapView.layer.sublayers count];
for (i = 0; i < count; i++) {
CALayer *layer = self.mapView.layer.sublayers[i];
if ([layer isKindOfClass:[CATextLayer class]]) {
[layer removeFromSuperlayer];
// NSLog(@"%@, %lu",[layer class],i);
} else {
// NSLog(@"%@",[layer class]);
}
}
// at here, some CATextLayer still in self.mapView.layer.sublayers
__block typeof(self) weakSelf = self;
NSDictionary *labelNameAndLocation = [self getLabelNameAndLocationInfo];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
[labelNameAndLocation enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id key, id obj, BOOL *stop) {
if ([(NSString *)key length] > 0) {
NSDictionary *location = (NSDictionary *)obj;
CATextLayer *label = [CATextLayer layer];
CGPoint caculateCenter = CGPointMake([weakSelf longitudeToCoordinate:[location[@"lng"] doubleValue]],[weakSelf latitudeToCoordinate:[location[@"lat"] doubleValue]]);
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]
initWithString:key
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],
NSParagraphStyleAttributeName:paragraphStyle,
NSForegroundColorAttributeName:[UIColor blackColor]}];
CGSize size = [text size];
[label setBounds:CGRectMake(0, 0, size.width, size.height)];
[label setString:text];
label.position = caculateCenter;
label.contentsScale = [[UIScreen mainScreen] scale];
[weakSelf.mapView.layer addSublayer:label];
}
}];
}
答案 0 :(得分:0)
你在删除过程中犯了经典的错误:
for (i = 0; i < count; i++) {
CALayer *layer = self.mapView.layer.sublayers[i];
if ([layer isKindOfClass:[CATextLayer class]]) {
[layer removeFromSuperlayer];
// NSLog(@"%@, %lu",[layer class],i);
} else {
// NSLog(@"%@",[layer class]);
}
}
将for
行更改为向下:
for (i = count-1; i >= 0; i--) {
原因是如果你从删除子层0开始,所有其他子层都向下移动一个索引。因此,如果删除子图层0并且子图层1也是文本图层,则该文本图层现在是子图层0并且永远不会被删除,因为您继续进行子图层1。因此,正如您正确演示的那样,您最终错过了一半他们。
事实上,我有点惊讶,因为这件事你没有崩溃,但我想有太多的子层你永远不会从阵列的末端掉下来;通常会发生崩溃。