remove all subLayers from a view

前端 未结 9 949
春和景丽
春和景丽 2021-02-01 01:16

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

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


        
9条回答
  •  时光取名叫无心
    2021-02-01 01:50

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

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

    Or, in Swift

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

提交回复
热议问题