我想为CALayer的子类设置动画。这一层绘制了一个“甜甜圈”图块。 我设定了一个开始 - & endAngle到图层。
我尝试过使用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;