【问题标题】:CALayer explicit animationCALayer 显式动画
【发布时间】:2014-04-29 15:23:21
【问题描述】:

伙计们,我正在尝试按照Apple's guide 执行 CABasicAnimation(仅用于测试目的)

有一段代码:

CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 5.0;
[theLayer addAnimation:fadeAnim forKey:@"opacity"];

// Change the actual data value in the layer to the final value.
theLayer.opacity = 0.0;

这表明我实际上应该在最后更改一个属性。但它似乎不能正常工作(它会立即改变不透明度) - 持续时间不是 5(我将其更改为 5 以获得更好的可见性)所以动画不是 CABasicAnimation 而是隐式的。 它仅在我将动画添加到layer 之前设置theLayer.opacity = 0.0; 时才有效。我做错了什么还是文档中的错误? P.S 运行最新的 XCode,iOS 7.1 模拟器。

【问题讨论】:

  • 在做动画之前更改属性或在动画结束时使用动画委托函数来做。
  • 添加fadeAnim.removedOnCompletion = NO;和 [fadeAnim setFillMode:kCAFillModeForwards];最终的不透明度应该在开始时设置。

标签: ios objective-c calayer caanimation


【解决方案1】:

在添加动画之前更新模型层。

CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 5.0;

// Change the actual data value in the layer to the final value.
theLayer.opacity = 0.0;

[theLayer addAnimation:fadeAnim forKey:@"opacity"];

【讨论】:

    猜你喜欢
    • 2010-11-23
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 2012-08-05
    • 1970-01-01
    • 2016-05-16
    相关资源
    最近更新 更多