0

我想为 CALayer 的子类制作动画。这一层绘制了一个“甜甜圈”图表。我为图层设置了一个开始和结束角度。

我试过 CABasicAnimation

我的图层

+ (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);
}

当我调用 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;
4

0 回答 0