【问题标题】:Why is this not animating?为什么这不是动画?
【发布时间】:2015-04-28 23:10:01
【问题描述】:

我在我的xcode 项目中实现了corePlot。我有一个要制作动画的饼图。这是我的代码:

- (void)configureChart
{
    CPTGraph *graph = self.hostView.hostedGraph;

    CPTPieChart *pieChart = [[CPTPieChart alloc] init];
    pieChart.dataSource = self;
    pieChart.delegate = self;
    pieChart.pieRadius = (self.hostView.bounds.size.height * 0.7) / 2;
    pieChart.startAngle = M_PI_4;
    pieChart.sliceDirection = CPTPieDirectionClockwise;

    [graph addPlot:pieChart];

这是我尝试过的:

    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotation.removedOnCompletion = YES;
    rotation.fromValue = [NSNumber numberWithFloat:0.0f];
    rotation.duration = 1.0f;
    rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotation.delegate = self;
    [pieChart addAnimation:rotation forKey:@"rotation"];
}

当我运行应用程序时,它没有动画。我做错了什么,我该如何解决?

更新 1

我尝试了以下方法:

[CPTAnimation animate:pieChart property:@"startAngle" from:pieChart.startAngle to:pieChart.endAngle duration:1.0];

这并没有达到预期的效果。图表会显示一秒钟,然后消失。

更新 2

我正在尝试获得这种效果:http://jsfiddle.net/ozgr1wfx/

我不确定自己做错了什么。

【问题讨论】:

  • 不需要给动画一个toValue吗?值应以弧度表示。使用DEGREES_TO_RADIANS(<value in degrees>)
  • 你是说这个#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
  • #define DEGREES_TO_RADIANS(degrees) (M_PI * (degrees) / 180.0)
  • 从您的演示链接中,旋转图像似乎是错误的。 @EricSkroch 下面的回答更有意义。使用 corePlot 类附带的动画来为适当的属性设置动画。
  • 你似乎知道 corePlot。你能给我举个例子吗?我不知道我做错了什么

标签: ios objective-c animation core-plot pie-chart


【解决方案1】:

不要为图层变换设置动画。而是为开始角度设置动画。否则,任何标签和其他注释都会随着绘图旋转。

来自 Plot Gallery 示例应用:

[CPTAnimation animate:piePlot
             property:@"startAngle"
                 from:CPTFloat(M_PI_2)
                   to:CPTFloat(M_PI_4)
             duration:0.25];

如 cmets 中所述,角度以弧度表示(pi 弧度 = 180 度)。

【讨论】:

  • 我怎样才能让它不显示任何东西然后在屏幕上顺时针动画?
  • “什么都不显示”是什么意思?您可以为startAngleendAngle 设置动画,以使图形如示例应用程序所示那样增长,或者如果需要,可以使用核心动画 (CA) 淡入整个绘图。您还可以使用 CA 为角度设置动画,因为它们只是 CGFloat 值。
  • 动画endAngle。让动画从startAngle 开始。
【解决方案2】:

感谢@RoryMcKinnel,我终于明白了。

[CPTAnimation animate:pieChart property:@"endAngle" from:pieChart.startAngle + M_PI * 2 to:pieChart.startAngle  duration:2.0];

这将使您的饼图动画化,如下所示:http://jsfiddle.net/ozgr1wfx/

再次。感谢@RoryMcKinnel 让我走上了正确的道路!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-21
    • 2013-07-11
    • 2013-02-03
    • 2012-04-28
    • 2012-06-02
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多