我制作了一个饼图类,现在卡住了试图制作它的动画。 找到了这个:https://github.com/pavanpodila/PieChart 我试图实现动画部分,但无法使动画生效。
添加饼图视图时,在以下设置的持续时间内没有可见的饼图切片:
PieChartLayer.m中的makeAnimationForKey:
但是在持续时间之后绘制切片。 所以现在它基本上就像延迟显示它并非意图:)
其他所有工作都应该如此。
试图摆弄一堆黑客......
- (PieSliceLayer *)filledCirleWithStartAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle withColor:(UIColor *)color {
PieSliceLayer *layer = [PieSliceLayer layer];
layer.frame = self.frame;
layer.fillColor = color;
layer.strokeColor = [UIColor clearColor];
layer.center = center;
layer.radius = radius;
layer.startAngle = DEGREES_TO_RADIANS(startAngle);
layer.endAngle = DEGREES_TO_RADIANS(endAngle);
return layer;
}
#import "PieSliceLayer.h"
@implementation PieSliceLayer
@dynamic startAngle, endAngle;
@synthesize fillColor, strokeColor, strokeWidth;
- (CABasicAnimation *)makeAnimationForKey:(NSString *)key {
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:key];
anim.fromValue = [[self presentationLayer] valueForKey:key];
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
anim.duration = 0.9f;
return anim;
}
- (id)init {
self = [super init];
if (self) {
self.fillColor = [UIColor grayColor];
self.strokeColor = [UIColor blackColor];
self.strokeWidth = 0;
[self setNeedsDisplay];
}
return self;
}
-(id<CAAction>)actionForKey:(NSString *)event {
if ([event isEqualToString:@"startAngle"] ||
[event isEqualToString:@"endAngle"]) {
return [self makeAnimationForKey:event];
}
return [super actionForKey:event];
}
- (id)initWithLayer:(id)layer {
if (self = [super initWithLayer:layer]) {
if ([layer isKindOfClass:[PieSliceLayer class]]) {
PieSliceLayer *other = (PieSliceLayer *)layer;
self.startAngle = other.startAngle;
self.endAngle = other.endAngle;
self.fillColor = other.fillColor;
self.strokeColor = other.strokeColor;
self.strokeWidth = other.strokeWidth;
}
}
return self;
}
+ (BOOL)needsDisplayForKey:(NSString *)key {
if ([key isEqualToString:@"startAngle"] || [key isEqualToString:@"endAngle"]) {
return YES;
}
return [super needsDisplayForKey:key];
}
-(void)drawInContext:(CGContextRef)ctx {
// Create the path
CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
CGFloat radius = self.radius;
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, center.x, center.y);
CGPoint p1 = CGPointMake(center.x + radius * cosf(self.startAngle), center.y + radius * sinf(self.startAngle));
CGContextAddLineToPoint(ctx, p1.x, p1.y);
int clockwise = self.startAngle > self.endAngle;
CGContextAddArc(ctx, center.x, center.y, radius, self.startAngle, self.endAngle, clockwise);
CGContextClosePath(ctx);
// Color it
CGContextSetFillColorWithColor(ctx, self.fillColor.CGColor);
CGContextSetStrokeColorWithColor(ctx, self.strokeColor.CGColor);
CGContextSetLineWidth(ctx, self.strokeWidth);
CGContextDrawPath(ctx, kCGPathFillStroke);
}
@end
答案 0 :(得分:1)
在initWithLayer方法中,您忘记设置半径。
self.radius = other.radius;