2012-07-21 14 views
5

Sto cercando di costruire un cerchio d'animazione che sarà disegnato in senso orario fino a quando diventa cerchio completo come illustrato nella iPhone Core Animation - Drawing a CircleCALayer come sottolivello non visibile

Il problema è che CALayer oggetto non viene aggiunto o costruire. Ho provato e ho visto che non sta accedendo ai miei metodi drawInContext:CGContextRef e animatingArc.

Quello che finora ho fatto è:

In AnimateArc.h

@interface AnimateArc : CALayer { 

CAShapeLayer *circle; 
} 

-(void) animatingArc; 

@end 

In AnimateArc.m

-(void) drawInContext:(CGContextRef)ctx 
{ 
CGFloat radius = 50.0; 
circle = [CAShapeLayer layer]; 

//make a circular shape 
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0, 0.0, 2 * radius, 2 * radius) cornerRadius:radius].CGPath; 

    CGPoint centerPoint = CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2);  

//center the shape in self.view 
circle.position = centerPoint; 

//configure appearence of circle 
circle.fillColor = [UIColor clearColor].CGColor; 
circle.strokeColor = [UIColor blackColor].CGColor; 
circle.lineWidth = 5;           

/*CGPointMake((self.contentsCenter.size.width), (self.contentsCenter.size.height));*/ 

//path the circle 
CGContextAddArc(ctx, centerPoint.x, centerPoint.y, radius, 0.0, 2 * M_PI, 0); 
CGContextClosePath(ctx); 

//fill it 
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor); 
CGContextFillPath(ctx); } 

//////// ////////////////////////////////////////////////// /////////////

-(void) animatingArc 
{ 
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"arcEnd"]; 
anim.duration = 20.0; //animate over 20 seconds 
anim.repeatCount = 1.0; //animate only once 
anim.removedOnCompletion = NO; //Reamin there after completion 

//animate from start to end 
anim.fromValue = [NSNumber numberWithFloat:50.0f]; 
anim.toValue = [NSNumber numberWithFloat:150.0f]; 

//experiment with timing to get appearence to look the way you want 
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 

//add animation to circle 
[circle addAnimation:anim forKey:@"animatingArc"]; 
} 

/////////////////////

//needed since key not part of animatable properties 
+(BOOL) needsDisplayForKey:(NSString *)key 
{ 
if([key isEqualToString:@"arcEnd"]) 
    return YES; 
else 
    return [super needsDisplayForKey:key]; 

} 

//ensure custom properties copied to presentation layer 
-(id) initWithLayer:(id)layer 
{ 
if((self = [super initWithLayer:layer])) 
{ 
    if ([layer isKindOfClass:[AnimateArc class]]) 
    { 
     AnimateArc *other = (AnimateArc *) layer; 
     [other setNeedsDisplay]; 
    } 
} 
return self; } 

E finalmente nel mio viewController,

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[self.view.layer addSublayer:AnimateArcObject]; 
[AnimateArcObject animatingArc]; 
} 

Apologia per la formattazione male .... Per favore qualcuno può dirmi cosa sto sbagliando? Ho anche il dubbio che il mio codice possa bloccarsi in qualsiasi posto dopo l'accesso a queste due funzioni poiché sono novizio su Core Animation e non ho idea che io sia nella giusta direzione o no.

Qualsiasi aiuto sarà apprezzato. Grazie.

risposta

0

Dopo una piccola ricerca, ho pensato che sto andando nella direzione sbagliata. Così ho cancellato questo file AnimateArc e ne ho aggiunto uno nuovo che è che eredita da UIViewController.

Quindi nel metodo viewDidLoad, ho scritto il codice da this link per creare cerchio e animazioni utilizzando il percorso.

Nel controller di visualizzazione genitore , ho aggiunto AnimatedArc ViewController's subview. Ora funziona perfettamente :)

16

Dalla mia esperienza dolorosa con Core Animation, è necessario sempre impostare la proprietà bounds di qualsiasiCALayer si crea un'istanza.

Così, sei strato non viene pubblicato perché vi manca qualcosa di simile:

layer.bounds = CGRectMake(0, 0, width, height);

si dovrebbe collocare questo non appena si crea un'istanza dello strato, e prendere l'abitudine di farlo, quindi non cadi di nuovo in esso.

Per quanto riguarda il codice in arresto, mi dispiace. È troppo distribuito e non sono sicuro di come sia collegato insieme, quindi non posso aiutarti.

+0

Grazie per la risposta.Ma l'impostazione dei limiti non risolveva ancora il mio problema :( – NightFury

+0

Mi ha aiutato, ma non l'ho risolto completamente. Il livello ora sta disegnando sullo schermo, ma è leggermente sfalsato rispetto alla vista/livello genitore anche quando ho impostato i limiti del livello su il frame delle viste genitore Mi manca qualcosa di fondamentale a quanto pare – tracicot

+2

@tracicot si __must__ imposta i limiti del livello all'origine '(0, 0)'. Quindi, usa la proprietà del centro per cambiare la posizione. il frame del livello direttamente per controllare la posizione – Mazyod