【问题标题】:Why don't my CALayers animate? Instead they just instantly jump into place with the new values为什么我的 CALayers 没有动画?取而代之的是,他们只是立即使用新值就位
【发布时间】:2019-11-01 16:51:33
【问题描述】:

我有一个带有渐变颜色效果的 CALayer。我想改变“位置”和“颜色”值来改变渐变。我正在尝试为它制作动画。

但使用以下代码,它会立即更改为新值。

    let gradientDuration = 0.25

    let locationAnimation = CABasicAnimation(keyPath: "locations")
    locationAnimation.fromValue = lowerGradientView.layer.locations
    locationAnimation.toValue = newLocations

    let colorsAnimation = CABasicAnimation(keyPath: "colors")
    colorsAnimation.fromValue = lowerGradientView.layer.colors
    colorsAnimation.toValue = newColors

    let animationGroup = CAAnimationGroup()
    animationGroup.duration = gradientDuration
    animationGroup.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    animationGroup.animations = [locationAnimation, colorsAnimation]

    lowerGradientView.layer.add(animationGroup, forKey: "locationAndColors")
    lowerGradientView.layer.locations = newLocations as [NSNumber]?
    lowerGradientView.layer.colors = newColors

我做错了什么导致图层不对更改进行动画处理?

【问题讨论】:

    标签: ios cocoa-touch core-animation calayer caanimation


    【解决方案1】:

    问题在于最后的这几行:

    lowerGradientView.layer.locations = newLocations as [NSNumber]?
    lowerGradientView.layer.colors = newColors
    

    他们实际上取消了动画。

    【讨论】:

      猜你喜欢
      • 2021-01-12
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多