【问题标题】:How can I get a CGPathRef from SVGkit? or better solution to create CGPaths for CAKeyframeAnimation?如何从 SVGkit 获取 CGPathRef?或更好的解决方案来为 CAKeyframeAnimation 创建 CGPaths?
【发布时间】:2012-01-29 02:16:46
【问题描述】:

我基本上需要一种以图形方式创建路径以导入我的应用程序的方法,以便我可以在 CAKeyframeAnimation 对象中将它们用作动画路径。我现在也有点缺乏时间来推出我自己的单独工具来做到这一点。所以我认为 SVGkit 可能是最快/最简单的方法。

但我现在无法正确添加构成整个路径的不同子路径。

更新:

到目前为止,我有这个:

  // l has already been setup and added to the main view layer:
  // it's the CALayer whose position I'm trying to animate over the path.


  SVGDocument *document = [SVGDocument documentNamed:@"pathtest.svg"];
  CALayer *f = [document layerTree];

  CGMutablePathRef path = CGPathCreateMutable();
  for(int i = 0; i < [f.sublayers count]; i++)
    CGPathAddPath(path, NULL, ((CAShapeLayer*)[f.sublayers objectAtIndex:i]).path);


  CAKeyframeAnimation *a = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  a.path = path;
  a.fillMode = kCAFillModeForwards;
  a.removedOnCompletion = NO;
  a.duration = 5.0;
  [l addAnimation:a forKey:nil];

上面的代码“几乎”正确(但不完全,有趣):构成我的路径的不同子路径被添加到主路径中。但是子路径似乎没有添加到先前子路径的末尾。也就是说,在运行动画时,CALayer 似乎平移了一段路径,然后跳转到另一个位置,从该位置开始沿着第二个子路径平滑平移,等等。对于构成主路径的所有子路径。

有什么想法吗?

【问题讨论】:

    标签: ios svg core-animation core-graphics


    【解决方案1】:
    1. 您应该搜索层树并且只提取 CAShapeLayer 的 - 没有其他的 CGPath
    2. 所有 CGPath 都是 - 根据定义! - 绝对的。如果您希望它们是相对的,则必须手动获取每条路径并将其复制并偏移 X,Y 像素,其中 X,Y 是当前点(上一条路径的结尾)
    3. SVGkit 即将发生重大变化,以更简单/更易于使用的 API(更接近 SVG 规范)。您可以在这里尝试一下:https://github.com/adamgit/SVGKit/tree/transforms - 但该分支目前正在“开发中”。它相当稳定,但有时会得到实验性提交。

    【讨论】:

      猜你喜欢
      • 2011-11-21
      • 1970-01-01
      • 2015-01-31
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多