【问题标题】:How do I animate the appearance of a bar chart in Core Plot?如何在 Core Plot 中为条形图的外观设置动画?
【发布时间】:2011-04-05 13:20:09
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios core-plot


    【解决方案1】:

    这就是我所做的:

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

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

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

      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 :) .
      

    【讨论】:

      【解决方案2】:

      为您的 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"];
      

      【讨论】:

        【解决方案3】:

        感谢 Michele 的精彩回答。

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

        https://github.com/mayuur/CoreplotAnimation

        享受编码!!!

        【讨论】:

          【解决方案4】:
          【解决方案5】:

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

          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"];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多