ios - 从 View 中删除所有子图层

在一个动画中,我向 view 添加了很多 sublayers,其中:

[self.view.layer addSublayer:layer1];
[self.view.layer addSublayer:layer2];

....

我想通过一个 Action 删除所有子层。 我已经尝试过提出类似问题的建议:

rootLayer.sublayers = nil;

但它不起作用......

你能帮帮我吗? 比你!

最佳答案

CALayer 对象的sublayers 属性返回数组的副本。将其设置为 no nil 对子层没有任何作用。然而,这会做:

for (CALayer *layer in self.view.layer.sublayers) {
    [layer removeFromSuperlayer];
}

或者,在 Swift 中

self.view.layer.sublayers?.forEach { $0.removeFromSuperlayer() }

https://stackoverflow.com/questions/10789766/

相关文章:

objective-c - 从 UIScrollView 中删除所有 subview ?

ios - 字符串的静态 NSArray - 如何/在哪里初始化 View Controller

ios - 如何从 CGPoint 和 CGSize 创建 CGRect?

ios - 如何删除 xcode 5 中的旧配置文件?

objective-c - Interface Builder 中的 IBOutletCollect

objective-c - 奇怪的错误 NSAssert

ios - MKMapView MKPointAnnotation 点击​​事件

objective-c - 分组的 UITableView 在底部有 20px 的额外填充

iphone - 如何删除应用程序指定目录中的所有文件?

objective-c - 检测平移手势结束