Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a CALayer is already added as a sublayer

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.

like image 914
abe Avatar asked May 07 '12 13:05

abe


3 Answers

Have you tried the superlayer property ? It should be nil if your layer isn't added anywhere.

like image 71
adig Avatar answered Nov 16 '22 17:11

adig


if (layer.superlayer == parentLayer) {
    ...
} else {
    ...
}
like image 41
Ole Begemann Avatar answered Nov 16 '22 19:11

Ole Begemann


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.

like image 6
Alberto Lopez Avatar answered Nov 16 '22 18:11

Alberto Lopez