0

I want to animate a subclass of CALayer. This layer drawes a "donut"-chart piece. I set an start- & endAngle to the layer.

I tried with CABasicAnimation

My Layer

+ (BOOL)needsDisplayForKey:(NSString *)key {
return [key isEqualToString:@"endAngle"] ? YES : [super needsDisplayForKey:key];
}

- (void)drawInContext:(CGContextRef)ctx {
    CGFloat lineWidth = self.circleRingWidth / 2;
    CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    CGFloat radius = (MIN(center.x-1, center.y-1) + lineWidth/2);

    CGContextBeginPath(ctx);
    CGContextSetAllowsAntialiasing(ctx, YES);

    CGContextAddArc(ctx, center.x, center.y, radius - kDefaultRingWidth, self.startAngle,      
                                                             self.endAngle, NO);

    CGContextSetBlendMode(ctx, kCGBlendModeColor);
    CGContextSetLineWidth(ctx, lineWidth);
    CGContextSetStrokeColorWithColor(ctx, self.color ? self.color.CGColor : [UIColor 
                                                             clearColor].CGColor);
    CGContextDrawPath(ctx, kCGPathStroke);
}

When i call setSelectedIndex

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"circleRingWidth"];
animation.duration = 2.0f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = [NSNumber numberWithFloat:DEFAULT_RING_WIDTH];
animation.toValue = [NSNumber numberWithFloat:SELECTED_RING_WIDTH];
[self.selectedLayer addAnimation:animation forKey:@"circleRingWidth"];
self.selectedLayer.circleRingWidth = SELECTED_RING_WIDTH;
Christian 'fuzi' Orgler
  • 1,682
  • 8
  • 27
  • 51
  • possible duplicate of [Animating a custom property of CALayer subclass](http://stackoverflow.com/questions/2395382/animating-a-custom-property-of-calayer-subclass) – titaniumdecoy Oct 14 '13 at 16:18
  • already implemented this kind of things.. didn't work - please give me a clean example or a link to an example if u know something. – Christian 'fuzi' Orgler Oct 14 '13 at 16:33
  • [My answer](http://stackoverflow.com/questions/19314355/iphone-where-to-put-code-regulating-calayer-animation/19320927#19320927) and the link to the github project might be helpful. – Arek Holko Oct 14 '13 at 16:40

0 Answers0