如何使图层保持CABasicAnimation的最终值 [英] How do I make a layer maintain the final value of a CABasicAnimation

查看:58
本文介绍了如何使图层保持CABasicAnimation的最终值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下动画:

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(最初设置的位置)。我如何使最终值 stick?

This will essentially animate the drawing of the layer from one end to the next. The problem is that once the animation completes, the strokeEnd property resets back to 0 (where it was initially set). How do I make the final value "stick"?

我试图在animationDidStop委托方法中更改此值。这通常可以正常工作,但是即使将其放入CATransaction中以禁用动画,也会在0处短暂地产生 strokeEnd 的闪烁。我也玩过加性累积性属性,但无济于事。

I have attempted to change this in the animationDidStop delegate method. This mostly works, but can cause a flash of strokeEnd at 0 briefly, even when put inside a CATransaction to disable animations. I have also played with the additive and cumulative properties to no avail. Any suggestions?

推荐答案

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

You simply set the strokeEnd property to its final value yourself! Like this:

// 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 是隐式动画的(以便该行本身会产生动画),我们将首先关闭隐式动画:

If strokeEnd were implicitly animatable (so that that line would itself cause animation) we would turn off implicit animation first:

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

请注意,您的 pathAnimation.removedOnCompletion = NO; 是错误的,另一个答案的 kCAFillModeForwards 也是如此。两者都是基于对Core Animation运作方式的误解。

Note that your pathAnimation.removedOnCompletion = NO; is wrong, and so is the other answer's kCAFillModeForwards. Both are based on misunderstandings of how Core Animation works.

这篇关于如何使图层保持CABasicAnimation的最终值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆