Iphone 如何为使用core plot library开发的饼图设置动画?

Iphone 如何为使用core plot library开发的饼图设置动画?,iphone,core-plot,Iphone,Core Plot,在iPhone应用程序Roambi中,显示的饼图可以设置动画,以便与用户一起旋转,就像旋转光盘一样。我们可以用它做很多事情 有人提到Roambi应用程序是使用core plot library开发的: 如何操作使用Core plot开发的饼图?对于简单的饼图,即使使用上述动画,也不需要Core plot。如果您只想要核心绘图解决方案,请忽略此答案。然而,这里有一个简单的解决方案,它不需要外部包,甚至不需要任何框架 创建名为PieChartView的UIView。在PieChartView.h中

在iPhone应用程序Roambi中,显示的饼图可以设置动画,以便与用户一起旋转,就像旋转光盘一样。我们可以用它做很多事情

有人提到Roambi应用程序是使用core plot library开发的:


如何操作使用Core plot开发的饼图?

对于简单的饼图,即使使用上述动画,也不需要Core plot。如果您只想要核心绘图解决方案,请忽略此答案。然而,这里有一个简单的解决方案,它不需要外部包,甚至不需要任何框架

创建名为PieChartView的UIView。在PieChartView.h中:

#import <UIKit/UIKit.h>

@interface PieChartView : UIView {
    float zeroAngle;
    NSMutableArray *valueArray;
    NSMutableArray *colorArray;
}
@property (nonatomic) float zeroAngle;
@property (nonatomic, retain) NSMutableArray *valueArray;
@property (nonatomic, retain) NSMutableArray *colorArray;
@end
#导入
@接口PieChartView:UIView{
浮动零角;
NSMutableArray*valueArray;
NSMutableArray*彩色数组;
}
@属性(非原子)浮点零角;
@属性(非原子,保留)NSMutableArray*valueArray;
@属性(非原子,保留)NSMutableArray*colorArray;
@结束
然后是PieChartView.m中的实现:

#import "PieChartView.h"

@implementation PieChartView
@synthesize zeroAngle;
@synthesize valueArray, colorArray;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    // DETERMINE NUMBER OF WEDGES OF PIE
    int wedges = [valueArray count];
    if (wedges > [colorArray count]) {
        NSLog(@"INSUFFICENT COLORS FOR PIE CHART: Please add %d additional colors.",wedges-[colorArray count]);
        for (int i=[colorArray count] ; i<wedges ; ++i) {
            [colorArray addObject:[UIColor whiteColor]];
        }
    }

    // DETERMINE TOTAL VOLUME OF PIE
    float sum = 0.0;
    for (int i=0; i<wedges ; ++i) {
        sum += [[valueArray objectAtIndex:i] floatValue];
    }
    float frac = 2.0*M_PI/sum;

    // DETERMINE CENTER AND MAXIMUM RADIUS OF INSCRIBED CIRCLE
    int center_x = rect.size.width/2.0;
    int center_y = rect.size.height/2.0;
    int radius = (center_x > center_y ? center_x : center_y);

    // DRAW WEDGES CLOCKWISE FROM POSITIVE X-AXIS
    float startAngle=zeroAngle;
    float endAngle=zeroAngle;
    for (int i=0; i<wedges; ++i) {
        startAngle = endAngle;
        endAngle += [[valueArray objectAtIndex:i] floatValue]*frac;
        [[colorArray objectAtIndex:i] setFill];
        CGContextMoveToPoint(context, center_x, center_y);
        CGContextAddArc(context, center_x, center_y, radius, startAngle, endAngle, 0);
        CGContextClosePath(context);
        CGContextFillPath(context);
    }
}

- (void)dealloc {
    [valueArray release];
    [colorArray release];
    [super dealloc];
}
@end
#导入“PieChartView.h”
@实现图表视图
@合成零角;
@综合数值数组、彩色数组;
-(id)initWithFrame:(CGRect)帧{
if((self=[super initWithFrame:frame])){
}
回归自我;
}
-(void)drawRect:(CGRect)rect{
CGContextRef context=UIGraphicsGetCurrentContext();
//确定饼图的楔块数
int wedges=[valueArray count];
如果(楔形>彩色阵列计数){
NSLog(@“饼图的颜色不足:请添加%d种其他颜色。”,楔形-[colorArray count]);

对于(int i=[colorArray count];我不知道CorePlot,在不久的将来它可能会对我有一些用处,所以我只是看了一下。CorePlot很有趣而且很活跃

关于CorePlot的动画:

  • CorePlot文档中提到了一个,,但尚未实施
  • 即使基于
    核心动画
    ,也不意味着CorePlot图形将很容易设置动画
  • 以下是您的选择(根据我的偏好顺序):

  • 寻找另一个像Roambi那样处理动画的绘图库
  • 基于CorePlot实现动画(意味着对该框架和
    核心动画有很好的理解)
  • 根据你想要的,你自己做每件事,可能也没那么难……但同样需要
    核心动画
    技能

  • 我怀疑Roambi是否使用CorePlot,但如果他们这样做,他们会将其作为一个拥有大量自定义内部代码的基础…

    快速查看Core Plot源代码可以发现,
    CPPieChart
    的继承如下所示:

    :::::

    因此,您可以看到,最后,
    CPPieChart
    只是一个高度子类化的
    CALayer
    。我在这里可能完全不正确,但没有任何迹象表明它不能像任何其他
    CALayer
    那样设置动画。请尝试以下代码将层旋转360度:

    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D transform = CATransform3DMakeRotation(DegreesToRadians(360), 0, 0, 1);
    rotation.toValue = [NSValue valueWithCATransform3D:transform];
    rotation.duration = 10.0f;
    [pieChart addAnimation:rotation forKey:@"rotation"];
    

    如果你能做到这一点,那么只需从加速度计读取值并将其转换为旋转角度即可。

    @PengOne感谢你编辑了我糟糕的语法,英语显然不是我的主要语言;)如果你只是做饼图,你可能想看看XYPIECART。它有动画,看起来更好(在我看来)比CorePlot。