【问题标题】:Seamlessly updating a layer's visual property values after a `CAAnimationGroup` completes?在“CAAnimationGroup”完成后无缝更新图层的视觉属性值?
【发布时间】:2015-06-05 22:28:40
【问题描述】:

我正在尝试为比例设置动画然后CALayer 的不透明度,如下所示:

CABasicAnimation *scaleUp = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleUp.fromValue = [NSValue valueWithCATransform3D:self.timerProgressLayer.transform];
scaleUp.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];
scaleUp.duration = 0.25;
scaleUp.fillMode = kCAFillModeForwards;

CABasicAnimation *fadeOut = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeOut.fromValue = @(1.0);
fadeOut.toValue = @(0.0);
fadeOut.beginTime = 0.3;
fadeOut.duration = 0.25;
fadeOut.fillMode = kCAFillModeForwards;

CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[scaleUp, fadeOut];
group.removedOnCompletion = YES;
group.duration = fadeOut.beginTime + fadeOut.duration;

[self.timerProgressLayer addAnimation:group forKey:@"trigger"];

这很简单,动画本身也可以正常工作。但是,在动画结束时,它会被移除,并且值会恢复为开始时的值。为了解决这个问题,我在addAnimation: 调用之后立即手动设置属性:

self.timerProgressLayer.opacity = 0.0;
self.timerProgressLayer.transform = CATransform3DMakeScale(1.0, 1.0, 1.0);

但是,这些调用会覆盖我的动画,并且图层会立即淡出并缩放。如果我在动画结束时使用动画的delegate[CATransaction setCompletionBlock:] 设置属性,很多时候(但不是100% 的时间),旧状态的单帧在结束之间通过动画和正在设置的属性。

我如何使用CAAnimationGroup 为某些属性设置动画,在最后删除动画,而不会看到帧的旧值?

【问题讨论】:

    标签: ios cocoa-touch core-animation


    【解决方案1】:

    我之前给过the long version of this answer。这里没有理由重复详细的解释。

    但简短的回答是,您看到图层的隐式动画被应用在显式动画之上。我之前写过a detailed explanation about implicit and explicit animations and multiple simulations animation,如果你想了解更多。


    如“长答案”中所述。您的问题的解决方案是更新属性,但暂时禁用隐式动画,以便它们不会应用到显式动画之上:

    [CATransaction begin];
    [CATransaction setDisableActions:YES]; // actions are disabled for now
    
    self.timerProgressLayer.opacity = 0.0;
    self.timerProgressLayer.transform = CATransform3DMakeScale(1.0, 1.0, 1.0);
    
    [CATransaction commit];                // until here
    

    【讨论】:

    • 糟糕,这是我尝试过的其中一件事——这样做完全可以忽略动画,立即在视觉上设置属性。
    • 您可能应该有一个向后或两者都填充模式,以便在等待开始时间过去时显示“旧”动画值。
    • 啊!修复填充模式解决了这个问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多