如何在 Core Plot 中为条形图的外观设置动画?

发布于 2024-10-30 14:54:14 字数 78 浏览 5 评论 0原文

当第一次使用 Core Plot 显示条形图时,我希望条形向上生长,直到达到正确的高度。

您将如何使用这个框架创建这样的动画?

When first displaying a bar chart using Core Plot, I'd like the bars to grow upward until they reach their correct heights.

How would you create such an animation using this framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小瓶盖 2024-11-06 14:54:14

这就是我所做的:

  1. 在我的 viewDidLoad 控制器的方法中,我创建了 CPTXYGraph:

    graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
    
  2. 我向空图表添加了动画:

    CAKeyframeAnimation *scale = [CAKeyframeAnimationanimationWithKeyPath:@"transform"];
    
  3. 然后在animationDidStop 委托的方法中,我向图表添加了绘图数据和动画:

    CABasicAnimation *anim = [CABasicAnimationanimationWithKeyPath:@"transform.scale.y"];
    [动画设置持续时间:2.0f];
    
    anim.toValue = [NSNumber numberWithFloat:1.0f];
    anim.fromValue = [NSNumber numberWithFloat:0.0f];
    anim.removedOnCompletion = NO;
    anim.delegate = self;
    anim.fillMode = kCAFillModeForwards;
    
    gPlot.anchorPoint = CGPointMake(0.0, 0.0);
    
    [gPlot addAnimation:anim forKey:@"grow"];
    
    [graph addPlot:gPlot ];// 重要的是,我将绘图数据添加到了图表中:)。
    

This is what I did:

  1. In my viewDidLoad controller's method I created the CPTXYGraph:

    graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
    
  2. I added animation to the empty graph:

    CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
  3. Then in animationDidStop delegate's method I added the plot data and animation to the graph:

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    [anim setDuration:2.0f];
    
    anim.toValue = [NSNumber numberWithFloat:1.0f];
    anim.fromValue = [NSNumber numberWithFloat:0.0f];
    anim.removedOnCompletion = NO;
    anim.delegate = self;
    anim.fillMode = kCAFillModeForwards;
    
    gPlot.anchorPoint = CGPointMake(0.0, 0.0);
    
    [gPlot addAnimation:anim forKey:@"grow"];
    
    [graph addPlot:gPlot ];// IMPORTANT here I added the plot data to the graph :) .
    
溺ぐ爱和你が 2024-11-06 14:54:14

为 CPTBarPlot 添加动画,如下所示:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
[animation setDuration:1];
CATransform3D transform = CATransform3DMakeScale(1, 0.0001, 1);
// offsetY=[PlotDisplayAreaUnderXAxisHeight]-[PlotDisplayAreaHeight]/2
transform = CATransform3DConcat(transform, CATransform3DMakeTranslation(0, offsetY, 0));
animation.fromValue = [NSValue valueWithCATransform3D:transform];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
[barPlot addAnimation:animation forKey:@"barGrowth"];

Add animation for your CPTBarPlot as follows:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
[animation setDuration:1];
CATransform3D transform = CATransform3DMakeScale(1, 0.0001, 1);
// offsetY=[PlotDisplayAreaUnderXAxisHeight]-[PlotDisplayAreaHeight]/2
transform = CATransform3DConcat(transform, CATransform3DMakeTranslation(0, offsetY, 0));
animation.fromValue = [NSValue valueWithCATransform3D:transform];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
[barPlot addAnimation:animation forKey:@"barGrowth"];
一个人的夜不怕黑 2024-11-06 14:54:14

感谢米歇尔的精彩回答。

我已将她的回答中的示例上传到 Github,供那些想要下载演示并立即开始工作的人使用!

https://github.com/mayuur/CoreplotAnimation

享受编码吧!

Thanks Michele for your great answer.

I have uploaded an example from her answer to Github for those who would want to download the demo and start working right away!

https://github.com/mayuur/CoreplotAnimation

Enjoy Coding!!!

流年已逝 2024-11-06 14:54:14

像这样添加不透明动画...

CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[xSquaredPlot addAnimation:fadeInAnimation forKey:@"animateOpacity"];

Add opacity animation like this...

CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[xSquaredPlot addAnimation:fadeInAnimation forKey:@"animateOpacity"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文