使用未解析的标识符'kCAFillModeForwards'

Jul*_* W. 3 xcode animation cabasicanimation caanimation swift

当我尝试设置animation.fillMode = kCAFillModeForwardsXcode时无法编译并显示错误." 使用未解析的标识符'kCAFillModeForwards'".

我已经在以前的项目中使用过这个没有任何问题,有人已经遇到过这种行为吗?

func animateGradient() {
        currentGradient += 1
        let animation = CABasicAnimation(keyPath: Animation.keyPath)
        animation.duration = animationDuration
        animation.toValue = currentGradientSet()
        animation.fillMode = kCAFillModeForwards
        animation.isRemovedOnCompletion = false
        animation.delegate = self
        gradient.add(animation, forKey: Animation.key)
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ist 8

该常量已被删除,有利于forwardsCAMediaTimingFillMode类型的属性.从Swift 4.2开始,同样的事情写成:

animation.fillMode = .forwards
Run Code Online (Sandbox Code Playgroud)

也就是说,前向填充模式的组合以及在完成时不移除动画的组合经常被误用于试图使动画"粘住"/"保持".除非您要为图层的移除设置动画,否则更清晰的解决方案是将图层更新为新值并添加动画 - 在完成时删除 - 从前一个值转换.