2012-06-03 18 views
22

Vorrei animare un UIBezierPath di un rect a un triangolo, non animare una vista su un percorso ma animare il percorso stesso in modo che si trasformi da una forma all'altra. il percorso viene aggiunto a un CALayerCome animare un UIBezierPath

CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.fillColor = [[UIColor whiteColor] CGColor];  
maskLayer.path = [self getRectPath]; 

-(CGPathRef)getTrianglePath 
{ 
    UIBezierPath* triangle = [UIBezierPath bezierPath]; 
    [triangle moveToPoint:CGPointZero]; 
    [triangle addLineToPoint:CGPointMake(width(self.view),0)]; 
    [triangle addLineToPoint:CGPointMake(0, height(self.view))]; 
    [triangle closePath]; 
    return [triangle CGPath]; 
} 

-(CGPathRef)getRectPath 
{ 
    UIBezierPath*rect = [UIBezierPath bezierPathWithRect:self.view.frame]; 
    return [rect CGPath]; 
} 

risposta

28

Io non sono un esperto di Core Animation, ma è possibile definire un CABasicAnimation come segue

CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"]; 
morph.duration = 5; 
morph.toValue = (id) [self getTrianglePath]; 
[maskLayer addAnimation:morph forKey:nil]; 

La prima riga indica che si desidera definire un'animazione che cambia il path proprietà del livello. La seconda riga indica che l'animazione impiega cinque secondi e la terza riga fornisce lo stato finale dell'animazione. Infine, aggiungiamo l'animazione al livello. La chiave è un identificatore univoco per l'animazione, ovvero se si aggiungono due animazioni con la stessa chiave, viene considerato solo l'ultimo. Questa chiave può anche essere usata per sovrascrivere le cosiddette animazioni implicite. Ci sono un paio di proprietà di un CALayer che sono animate di default. Più precisamente, se si modifica una di queste proprietà, la modifica verrà animata con una durata di 0,25.

+0

grazie, semplice. stavo usando CAKeyframeAnimation. –