gpt4 book ai didi

ios - CABasicAnimation 旋转返回到原始位置

转载 作者:可可西里 更新时间:2023-11-01 06:18:36 25 4
gpt4 key购买 nike

我正在使用 CABasicAnimation 旋转 CALayer 并且工作正常。问题是,当我尝试旋转同一层时,它会在旋转之前返回到原来的位置。我的预期输出是,对于下一次轮换,它应该从它结束的地方开始。这是我的代码:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.fromValue = 0;
animation.toValue = [NSNumber numberWithFloat:3.0];
animation.duration = 3.0;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.autoreverses = NO;
[calayer addAnimation:animation forKey:@"rotate"];

我的代码中是否缺少任何内容?谢谢

最佳答案

发生的事情是您在表示层中看到了动画。但是,这不会更新图层的实际位置。因此,动画结束后,您会看到图层的原样,因为它没有发生变化。

"Core Animation Rendering Architecture" 真的值得一读.否则这可能会非常困惑。

要修复它,请按如下方式为您的 CABasicAnimation 设置委托(delegate):

[动画 setDelegate:self];

然后,创建一个方法来设置动画完成时所需的目标属性。现在,这是令人困惑的部分。您应该在 animationDidStart 而不是 animationDidStop 上执行此操作。否则,表示层动画将结束,当您在原始位置看到 calayer 时会出现闪烁,然后它会跳转(没有动画)到目标位置。用 animationDidStop 试试,你就会明白我的意思了。

我希望这不会太困惑!

- (void)animationDidStart:(CAAnimation *)theAnimation
{
[calayer setWhateverPropertiesExpected];
}

编辑:

我后来发现 Apple 推荐了一种更好的方法来做到这一点。

Oleg Begemann 在他的博客文章 Prevent Layers from Snapping Back to Original Values When Using Explicit CAAnimations 中对正确的技术进行了很好的描述。

基本上你所做的是在开始动画之前,记下图层的当前值,即原始值:

// Save the original value
CGFloat originalY = layer.position.y;

接下来,在层的模型上设置toValue。因此,图层模型具有您将要执行的任何动画的最终值:

// Change the model value
layer.position = CGPointMake(layer.position.x, 300.0);

然后,您将动画设置为 fromValue 是您在上面记下的原始值:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];

// Now specify the fromValue for the animation because
// the current model value is already the correct toValue
animation.fromValue = @(originalY);
animation.duration = 1.0;

// Use the name of the animated property as key
// to override the implicit animation
[layer addAnimation:animation forKey:@"position"];

请注意,为清楚起见,上面编辑中的代码是从 Ole Begemann 的博客复制/粘贴的

关于ios - CABasicAnimation 旋转返回到原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7683526/

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