ios 从视图中删除所有子图层

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10789766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:31:36  来源:igfitidea点击:

remove all subLayers from a view

iosobjective-ccalayer

提问by Beppino66

In an animation I added a lot of sublayersto a view, with:

在动画我加了很多的子层观点,有:

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

....

....

I would like to remove all sublayers with an action. I already tried with this suggestion of a similar question:

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

rootLayer.sublayers = nil;

but it doesn't work...

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

Could you help me? Than you!

你可以帮帮我吗?比你!

回答by Svein Halvor Halvorsen

The sublayersproperty of a CALayerobject returns a copy of the array. Setting it no nil does nothing about the sublayers. This however will do:

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

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

Or, in Swift

或者,在 Swift 中

self.view.layer.sublayers?.forEach { 
view.layer.sublayers = nil
.removeFromSuperlayer() }

回答by Harshil Kotecha

Swift 3.0 & Swift 4.0

斯威夫特 3.0 和斯威夫特 4.0

Set the sublayersproperty to nilto remove all sublayers from a view.

将该sublayers属性设置为nil从视图中删除所有子图层。

.removeAll()

also you can add

你也可以添加

[self.view.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)]

回答by Hussain1982

This worked for me and fixed the crash:

这对我有用并修复了崩溃:

    for layer: CALayer in self.view.layer.sublayers! {
        layer.removeFromSuperlayer()
    }

I changed the view with my image UImageview, and the crash is gone.

我用我的图像更改了视图,UImageview崩溃消失了。

回答by A.G

Swift 2.0:

斯威夫特 2.0:

    self.view.layer.performSelector("removeFromSuperlayer")

or

或者

layer.removeAllAnimations()
layer.removeFromSuperlayer()

回答by Matias Jurfest

Swift 5:

斯威夫特 5:

You can either remove the layer itself or iterate through them and do the following:

您可以删除图层本身或遍历它们并执行以下操作:

rootLayer.sublayers = [layer1] // adding one layer

rootLayer.sublayers = [layer1, layer2, ...] // adding two or more layers

回答by JonJ

Swift 4.1

斯威夫特 4.1

self.view.layer.sublayers?.removeAll()

self.view.layer.sublayers?.removeAll()

or if in a UIView sub-class just

或者如果在 UIView 子类中

layer.sublayers?.removeAll()

layer.sublayers?.removeAll()

回答by Sean Stayns

If you want to delete all sublayers and add a new one, you can easily do:

如果您想删除所有子图层并添加一个新的子图层,您可以轻松地执行以下操作:

self.view.layer.sublayers.removeAll()

This could be helpful, if you work with tableviewor collectionview cells. You don't need to call prepareForReusefor removingthe sublayers.

如果您使用tableview或,这可能会有所帮助collectionview cells。你不需要调用prepareForReuse消除sublayers

回答by Rmalmoe

Simple one liner in SWIFT 4.

SWIFT 4 中的简单衬里。

##代码##