Removing all CALayer's sublayers

前端 未结 15 1042
说谎
说谎 2020-12-07 20:25

I have trouble with deleting all of layer\'s sublayers. I currently do this manually, but that brings unnecessary clutter. I found many topics about this in google, but no a

15条回答
  •  死守一世寂寞
    2020-12-07 20:42

    Both setting self.view.layer.sublayers = nil and [layer removeFromSuperlayer] will result in crash if UIView contains any subview. Best way to remove CALayer from superLayer is by maintaining an array of layers. For instance that array is arrayOfLayers,

    Everytime you add a layer on UIView.sublayer add that layer in this array...

    For example

    [arrayOfLayers addObject:layer];
    [self.view.layer addSublayer:layer];
    

    While removing just call this,

    [arrayOfLayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
    

提交回复
热议问题