I have 5 CALayers each one is a property. Say I added 3 of them as subviews. I need to ba able to chk if one of the layers is already added to the layer.
Have you tried the superlayer
property ? 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With