在使用CALayer或者CAShapeLayer的时候,用addLayer插入很多图层以后,想要清空它的subLayers,例如:
for (CAShapeLayer *layer in self.candleLayer.sublayers)
{
[layer removeFromSuperlayer];
}
[self.ohlcLayer removeFromSuperlayer];
self.ohlcLayer = nil;
或者是这样:
for (CALayer *layer in self.candleLayer.sublayers)
{
[layer removeFromSuperlayer];
}
[self.ohlcLayer removeFromSuperlayer];
self.ohlcLayer = nil;
因为在使用其它类似操作时,都是这样干的,但这里不可以,因为报错了:
基本意思是说这里的sublayers不是可变数组,不能这样操作。所以这里提供2中解决方法:
1、直接赋空sublayers
self.candleLayer.sublayers = nil;
[self.candleLayer removeFromSuperlayer];
self.candleLayer = nil;
2、使用 makeObjectsPerformSelector
方法
[self.candleLayer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
[self.candleLayer removeFromSuperlayer];
self.candleLayer = nil;
作者:yunkai666 发表于2017/6/5 22:33:14 原文链接
阅读:199 评论:0 查看评论