gpt4 book ai didi

ios - 如何使图层保持 CABasicAnimation 的最终值

转载 作者:行者123 更新时间:2023-11-28 18:03:48 25 4
gpt4 key购买 nike

考虑以下动画:

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
pathAnimation.removedOnCompletion = NO;
pathAnimation.delegate = self;

这将从本质上使图层的绘制从一端动画到另一端。问题在于,一旦动画完成,strokeEnd 属性将重置回 0(最初设置的位置)。如何使最终值“稳定”?

我试图在 animationDidStop 委托(delegate)方法中更改它。这主要是有效的,但可能会导致 strokeEnd 在 0 处短暂闪烁,即使放在 CATransaction 中以禁用动画也是如此。我还玩过 additivecumulative 属性,但无济于事。有什么建议吗?

最佳答案

您只需自己将 strokeEnd 属性设置为其最终值!像这样:

// your current code (stripped of unnecessary stuff)

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];

// just add this to it:

theLayer.strokeEnd = 1;

// and now add the animation to theLayer

如果 strokeEnd 是隐式可动画的(以便该行本身会导致动画),我们将首先关闭隐式动画:

[CATransaction setDisableActions:YES];
theLayer.strokeEnd = 1;

请注意,您的 pathAnimation.removedOnCompletion = NO; 是错误的,其他答案的 kCAFillModeForwards 也是错误的。两者都是基于对 Core Animation 工作原理的误解。

关于ios - 如何使图层保持 CABasicAnimation 的最终值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16326623/

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