两个CGPath / UIBeziers之间的补间/插补

我有一个包含CGPath的CAShapeLayer。 使用iOS的内置animation,我可以使用animation轻松地将此path变形为另一个:

CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"]; morph.fromValue = (id)myLayer.path; mayLayer.path = [self getNewPath]; morph.toValue = (id)myLayer.path; morph.duration = 0.3; [myLayer addAnimation:morph forKey:nil]; 

这是完美的。

但是,现在我想在拖动操作期间逐渐在这些path之间变形。 为了做到这一点,我需要能够在拖动过程中的任何点检索插入的path。 有没有办法问这个iOS?

这实际上是很简单的,那么你会首先想到和使用animation“没有”的速度(暂停)。 现在,将暂停的animation添加到图层中,您可以更改时间偏移以跳转到animation中的特定时间。

如果你不打算自己运行animation(即只手动控制),我build议你将animation的持续时间改为1.这意味着当从0%移动到0时,你将时间偏移从0改变为1 100%。 如果你的持续时间是0.3(如你的例子),那么你将时间偏移设置为0和0.3之间的值。

履行

正如我上面所说,这个小窍门涉及的代码很less。

  1. (可选)将持续时间设置为1.0
  2. 设置层的speed (是的,他们符合CAMediaTiming )或animation0.0
  3. 在拖动手势或滑块期间(如我的示例)设置图层或animation的timeOffset (取决于您在第2步中所做的操作)。

这是我如何configuration我的animation+形状图层

 CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"]; morph.duration = 1.; // Step 1 morph.fromValue = (__bridge id)fromPath; morph.toValue = (__bridge id)toPath; [self.shapeLayer addAnimation:morph forKey:@"morph shape back and forth"]; self.shapeLayer.speed = 0.0; // Step 2 

和滑块动作:

 - (IBAction)sliderChanged:(UISlider *)sender { self.shapeLayer.timeOffset = sender.value; // Step 3 } 

你可以看到下面的最终结果。 快乐的编码!

在这里输入图像说明