5

我试图CALayer在几微秒后隐藏 a 并且我正在使用CABasicAnimation动画隐藏。

目前我正在尝试使用

[aLayer setHidden:YES];

CABasicAnimation * hideAnimation = [CABasicAnimation animationWithKeyPath:@"hidden"];
[hideAnimation setDuration:aDuration];
[hideAnimation setFromValue:[NSNumber numberWithBool:NO]];
[hideAnimation setToValue:[NSNumber numberWithBool:YES]];
[hideAnimation setBeginTime:0.09];
[hideAnimation setRemovedOnCompletion:NO];
[hideAnimation setDelegate:self];

[alayer addAnimation:hideAnimation forKey:@"hide"];

但是当我运行它时,图层会立即隐藏,而不是等待所需的开始时间。

我不确定我的 keyPath 是否为“隐藏”,但找不到任何其他选项,并且文档确实声明 a 的hidden属性CALayer是可动画的。

什么是实现我正在寻找的正确方法?

4

5 回答 5

3

请尝试为 opacity 属性设置动画。从 1.0 到 0.0,你应该得到你想要的效果。

于 2010-07-15T22:52:41.077 回答
3

从 CAMediaTiming.h,它说 beginTime 属性:

对象的开始时间,相对于其父对象(如果适用)。默认为 0。

You should use CACurrentMediaTime() + desired time offset.

[hideAnimation setBeginTime:CACurrentMediaTime() + 0.09];
于 2010-11-17T14:29:14.870 回答
2

I'm sure this is too late to do the original poster any good, but it may help others. I've been trying to do something similar, except to make the animation implicit when the hidden property is changed. As Tom says, animating opacity doesn't work in that case, as the change to the layer's hidden property seems to take effect right away (even if I delay the animation with beginTime).

The standard implicit action uses a fade transition (CATransition, type = kCATransitionFade), but this operates on the whole layer and I want to perform another animation at the same time, which is not a compatible operation.

After much experimentation, I finally noticed @Kevin's comment above and --- hello! --- that actually works! So I just wanted to call it out so the solution is more visible to future searchers:

CAKeyframeAnimation* hiddenAnim = [CAKeyframeAnimation animationWithKeyPath:@"hidden"];
hiddenAnim.values = @[@(NO),@(YES)];
hiddenAnim.keyTimes = @[@0.0, @1.0];
hiddenAnim.calculationMode = kCAAnimationDiscrete;
hiddenAnim.duration = duration;

This delays the hiding until the end of the duration. Combine it with other property animations in a group to have their effects seen before the layer disappears. (You can combine this with an opacity animation to have the layer fade out, while performing another animation.)

Thank you, Kevin!

于 2013-12-17T21:48:54.010 回答
2

swift 4

    let keyframeAnimation = CAKeyframeAnimation(keyPath: "hidden")
keyframeAnimation.calculationMode = kCAAnimationDiscrete
keyframeAnimation.repeatCount = 1.0
keyframeAnimation.values = [true, false,true,false,true]
keyframeAnimation.keyTimes = [0.0, 0.25,0.5,0.75, 1.0]
keyframeAnimation.duration = 30.0 //duration of the video in my case
keyframeAnimation.beginTime = 0.1
keyframeAnimation.isRemovedOnCompletion = false
keyframeAnimation.fillMode = kCAFillModeBoth

textLayer.add(keyframeAnimation, forKey:  "hidden")
于 2018-06-10T22:57:45.353 回答
0
    CABasicAnimation *endAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    endAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [endAnimation setFromValue:[NSNumber numberWithFloat:1]];
    [endAnimation setToValue:[NSNumber numberWithFloat:0.0]];
    [endAnimation setBeginTime:AVCoreAnimationBeginTimeAtZero];
    endAnimation.duration            = 5;
    endAnimation.removedOnCompletion = NO;
    [alayer addAnimation:endAnimation forKey:nil];
于 2017-04-14T05:23:29.323 回答