Removing all CALayer's sublayers

前端 未结 15 995
说谎
说谎 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:29

    The following should work:

    for (CALayer *layer in [[rootLayer.sublayers copy] autorelease]) {
        [layer removeFromSuperlayer];
    }
    
    0 讨论(0)
  • 2020-12-07 20:34

    Calling rootLayer.sublayers = nil; can cause a crash (e.g. if, under iOS 8, you call removeFromSuperview twice on the view owning rootLayer).

    The right way should be:

    [[rootLayer.sublayers copy] makeObjectsPerformSelector:@selector(removeFromSuperlayer)]

    The call to copy is needed so that the array on which removeFromSuperlayer is iteratively called is not modified, otherwise an exception is raised.

    0 讨论(0)
  • 2020-12-07 20:36
    [rootLayer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
    
    0 讨论(0)
  • 2020-12-07 20:36

    I had to do this in Xamarin/C#. I had a UITableViewCell with some CAShapeLayers for borders. All of the above options (including copying the Array and then removing layers caused a crash). The approach that worked for me:

    When adding the CALayer, I gave it a name:

        var border = new CALayer();
        border.BackgroundColor = color.CGColor;
        border.Frame = new  CGRect(x, y, width, height);
        border.Name = "borderLayerName";
        Layer.AddSublayer(border);
    

    In PrepareForReuse on the UITableViewCell, I created a copy of the SubLayers property and removed anything that matched the name I assigned earlier:

        CALayer[] copy = new CALayer[Layer.Sublayers.Length];
        Layer.Sublayers.CopyTo(copy, 0);
        copy.FirstOrDefault(l => l.Name == "borderLayerName")?.RemoveFromSuperLayer();
    

    No crashes.

    Hope this helps!

    0 讨论(0)
  • 2020-12-07 20:37

    You could simply provide an identifier for the CAlayer you have added and remove it by searching for it. Like so

     let spinLayer = CAShapeLayer()
     spinLayer.path = UIBezierPath()
     spinLayer.name = "spinLayer"
    
    
      func removeAnimation() {
         for layer in progressView.layer.sublayers ?? [CALayer]()
             where layer.name == "spinLayer" {
               layer.removeFromSuperlayer()
           }
       }
    
    0 讨论(0)
  • 2020-12-07 20:37

    What about doing:

    rootLayer.sublayers = @[];
    
    0 讨论(0)
提交回复
热议问题