2012-05-07 13 views

risposta

10

Hai provato la proprietà superlayer? Dovrebbe essere nullo se il tuo livello non viene aggiunto da nessuna parte.

+1

thx testato lo strato contro il super-nil lavorato solo di destra – abe

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

view.layer.sublayers ti dà una serie di strati di sub, per vedere se il livello è stato aggiunto che si può fare qualcosa di simile view.layer.sublayers.count e una volta che il conteggio livello raggiunge quello che ci si aspetta dont aggiungere altro per es.

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

È inoltre possibile esaminare ciascun livello nella matrice di sottolivelli per identificare meglio il livello che si sta cercando. Dal momento che sono proprietà dovresti essere in grado di fare un confronto con ciascuno dei livelli dell'array per vedere se il layer che stai cercando è stato aggiunto.

0
  • // per controllare CALayer Contiene sottolivello (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") 
    
                  } 
                 } 
    
                } 
               } 
              } 
    
Problemi correlati