简体   繁体   English

检查CALayer是否已添加为子层

[英]Check if a CALayer is already added as a sublayer

I have 5 CALayers each one is a property. 我有5个CALayers,每个是一个属性。 Say I added 3 of them as subviews. 假设我将其中3个添加为子视图。 I need to ba able to chk if one of the layers is already added to the layer. 如果其中一层已经添加到该层,我需要检查一下。

Have you tried the superlayer property ? 您是否尝试过superlayer属性? It should be nil if your layer isn't added anywhere. 如果您的图层未添加到任何地方,则应该为零。

if (layer.superlayer == parentLayer) {
    ...
} else {
    ...
}

view.layer.sublayers gives you an array of the sub layers, to see if your layer was added you can do something like view.layer.sublayers.count and once the layer count reaches what you expect dont add more for ex. view.layer.sublayers为您提供了一个子图层数组,以查看是否添加了图层,您可以执行类似于view.layer.sublayers.count的操作,一旦图层计数达到您的期望,就不要为ex添加更多内容。

if (view.layer.sublayers.count  < 3) {
//add layer
}else{
// do nothing because the layer has already been added.
}

You can also examine each layer in the sublayer array to better identify the layer you are looking for. 您还可以检查子图层阵列中的每个图层,以更好地标识您要查找的图层。 Since they are properties you should be able to do a comparison to each of the layers in the array to see if the layer you are looking for has been added. 由于它们是属性,因此您应该能够与数组中的每个图层进行比较,以查看是否已添加要查找的图层。

  • //to check CALayer Contains Sublayer(shpapelayer/textlayer) //检查CALayer是否包含子图层(shpapelayer / textlayer)

      if myShapeLayer.sublayers?.count>0 { var arr:NSArray? = myShapeLayer.sublayers as NSArray var i:Int=0 for i in 0..<arr!.count { var a: AnyObject = arr!.objectAtIndex(i) if a.isKindOfClass(CAShapeLayer) || a.isKindOfClass(CATextLayer) { if a.isKindOfClass(CAShapeLayer) { a = a as! CAShapeLayer if CGPathContainsPoint(a.path, nil, pointOfCircle, true) { NSLog("contains shape layer") } else { NSLog("not contains shape layer") } } if a.isKindOfClass(CATextLayer) { a = a as! CATextLayer var fr:CGRect = a.frame as CGRect if CGRectContainsPoint(fr, pointOfCircle) { NSLog("contains textlayer") } else { NSLog("not contains textlayer") } } } } } 

I needed to check to see if a gradientLayer was a subLayer of another view. 我需要检查一下渐变图层是否是另一个视图的子图层。 It was the only layer in there so I didn't have to check for anything else. 它是那里唯一的一层,因此我不必检查其他任何东西。 The answers above didn't work for me. 上面的答案对我不起作用。

I came across this answer and even though it was used for a different reason it was an easy way to check if the gradientLayer was a child of another view's layer property (the parentLayer) and it works fine for me: 我遇到了这个答案 ,尽管使用它的原因有所不同,但它还是一种很容易的方法,可以用来检查gradientLayer是否为另一个视图的图层属性(parentLayer)的子代,并且对我来说效果很好:

if let _ = (yourParentView.layer.sublayers?.compactMap { $0 as? CAGradientLayer })?.first {

    print("the gradientLayer IS already a subLayer of this parentView's layer property")
} else {

    print("the gradientLayer IS NOT a subLayer of this parentView's layer property")
}

XCode 11 - Swift 5 XCode 11-迅捷5

if view.layer.sublayers == nil {

    // add Sublayer

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM