gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:29:39 26 4
gpt4 key购买 nike

我正在尝试实现一个动画,当你按住一个按钮时,它会向下移动一个 block ,当你松开它时,它会将它动画回原来的位置,但我无法获得动画 block 的当前位置否有什么关系。这是我的代码:

-(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)你显然不明白 Core Animation 是如何工作的。事实证明这是非常普遍的,所以如果我错了请原谅我。不管怎样,请继续阅读,因为我将解释 Core Animation 的工作原理以及如何解决您的问题。

在 Core Animation 中,您通常使用的层对象是一个模型层。当您将动画附加到层时,Core Animation 会创建模型层的副本,称为表示层,动画会随时间改变表示层的属性。动画永远不会改变模型层的属性。

当动画结束并且(默认情况下)被移除时,表示层被销毁并且模型层的属性值再次生效。因此,屏幕上的图层似乎“弹回到”其原始位置/颜色/其他任何内容。

解决此问题的一种常见但错误方法是将动画的 removedOnCompletion 设置为 NO 及其 fillModekCAFillModeForwards。当您执行此操作时,表示层将悬空,因此屏幕上不会出现“回弹”。问题是现在您的表示层与模型层具有不同的值。如果您向模型层(或拥有它的 View )询问动画属性的值,您将得到一个与屏幕上显示的值不同的值。如果您再次尝试为该属性设置动画,动画可能会从错误的位置开始。

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

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

- (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];
}

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

当然,一旦你开始玩这个,你就会发现如果你moveUp:moveDown:只完成一半时, 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];
}

您可以找到我的测试程序的完整代码 in this gist .只需创建一个新的单 View 应用程序项目并将 ViewController.m 的内容替换为要点的内容即可。

关于ios - 动画期间无法获取 CALayer 的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13888926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com