动画期间无法获取CALayer的当前位置



我正在尝试实现一个动画,当你按下按钮时,它会向下动画化一个块,当你释放时,它又会将其动画化回原始位置,但无论怎样,我都无法获得动画化块的当前位置。这是我的代码:

-(IBAction)moveDown:(id)sender{
CGRect position = [[container.layer presentationLayer] frame];
[movePath moveToPoint:CGPointMake(container.frame.origin.x, position.y)];
[movePath addLineToPoint:CGPointMake(container.frame.origin.x, 310)];
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = NO;
moveAnim.fillMode = kCAFillModeForwards;

CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim, nil];
animGroup.duration = 2.0;
animGroup.removedOnCompletion = NO;
animGroup.fillMode = kCAFillModeForwards;
[container.layer addAnimation:animGroup forKey:nil];
}
-(IBAction)moveUp:(id)sender{
CGRect position = [[container.layer presentationLayer] frame];
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:CGPointMake(container.frame.origin.x, position.y)];
[movePath addLineToPoint:CGPointMake(container.frame.origin.x, 115)];
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = NO;
moveAnim.fillMode = kCAFillModeForwards;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim, nil];
animGroup.duration = 2.0;
animGroup.removedOnCompletion = NO;
animGroup.fillMode = kCAFillModeForwards;
[container.layer addAnimation:animGroup forKey:nil];
}

但是

CGRect position = [[container.layer presentationLayer] frame];

只是返回目的地位置而不是当前位置。一旦我释放按钮,我基本上需要给我容器的当前位置,这样我就可以执行下一个动画了。我现在拥有的东西不起作用。

我还没有对您的代码进行足够的分析,无法100%确定为什么[[container.layer presentationLayer] frame]可能不会返回您期望的结果。但我看到了几个问题。

一个明显的问题是moveDown:没有声明movePath。如果movePath是一个实例变量,您可能希望在每次调用moveDown:时清除它或创建一个新实例,但我不认为您会这样做。

一个不太明显的问题是(从你使用removedOnCompletionfillMode的情况来看,尽管你使用了presentationLayer)你显然不了解核心动画是如何工作的。事实证明,这种情况非常普遍,所以如果我错了,请原谅我。无论如何,请继续阅读,因为我将解释核心动画是如何工作的,然后如何解决您的问题。

在核心动画中,通常使用的层对象是模型层。将动画附加到层时,核心动画会创建模型层的副本,称为表示层,并且动画会随着时间的推移更改表示层的属性。动画永远不会更改模型层的特性。

当动画结束并(默认情况下)被移除时,表示层将被破坏,模型层属性的值将再次生效。因此,屏幕上的图层显示为"快速返回"到其原始位置/颜色/其他位置。

解决此问题的常见但错误的方法是将动画的removedOnCompletion设置为NO,将其fillMode设置为kCAFillModeForwards。当您这样做时,表示层会挂起,因此屏幕上不会出现"回拍"。现在的问题是,表示层的值与模型层的值不同。如果向模型层(或拥有它的视图)询问动画特性的值,则会得到与屏幕上不同的值。如果再次尝试设置属性的动画,动画可能会从错误的位置开始。

若要设置层特性的动画并使其"粘滞",需要更改模型层的特性值,然后应用动画。这样,当动画被移除,表示层消失时,屏幕上的层将看起来完全相同,因为模型层的属性值与其表示层在动画结束时的属性值相同。

现在,我不知道为什么要使用关键帧来设置直线运动的动画,或者为什么要使用动画组。两者在这里似乎都没有必要。您的两种方法实际上是相同的,所以让我们考虑一下共同的代码:

- (void)animateLayer:(CALayer *)layer toY:(CGFloat)y {
CGPoint fromValue = [layer.presentationLayer position];
CGPoint toValue = CGPointMake(fromValue.x, y);
layer.position = toValue;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:fromValue];
animation.toValue = [NSValue valueWithCGPoint:toValue];
animation.duration = 2;
[layer addAnimation:animation forKey:animation.keyPath];
}

请注意,当我将动画添加到层中时,我们为其提供了一个关键帧。由于我们每次都使用相同的关键帧,如果先前的动画尚未完成,则每个新动画都将替换(移除)先前的动画。

当然,只要你玩这个,你就会发现,如果你在moveDown:只完成了一半的时候moveUp:moveUp:动画将以一半的速度出现,因为它仍然有2秒的持续时间,但只有一半的距离。我们真的应该根据要行驶的距离来计算持续时间:

- (void)animateLayer:(CALayer *)layer toY:(CGFloat)y withBaseY:(CGFloat)baseY {
CGPoint fromValue = [layer.presentationLayer position];
CGPoint toValue = CGPointMake(fromValue.x, y);
layer.position = toValue;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:fromValue];
animation.toValue = [NSValue valueWithCGPoint:toValue];
animation.duration = 2.0 * (toValue.y - fromValue.y) / (y - baseY);
[layer addAnimation:animation forKey:animation.keyPath];
}

如果您真的需要它成为动画组中的关键路径动画,那么您的问题应该向我们展示为什么您需要这些东西。不管怎样,它也适用于这些东西:

- (void)animateLayer:(CALayer *)layer toY:(CGFloat)y withBaseY:(CGFloat)baseY {
CGPoint fromValue = [layer.presentationLayer position];
CGPoint toValue = CGPointMake(fromValue.x, y);
layer.position = toValue;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:fromValue];
[path addLineToPoint:toValue];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path.CGPath;
animation.duration =  2.0 * (toValue.y - fromValue.y) / (y - baseY);
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = animation.duration;
group.animations = @[animation];
[layer addAnimation:group forKey:animation.keyPath];
}

你可以在这个要点中找到我的测试程序的完整代码。只需创建一个新的单视图应用程序项目,并将ViewController.m的内容替换为要点的内容。

最新更新