【发布时间】:2014-06-26 16:50:59
【问题描述】:
我正在尝试为视图的背景颜色设置动画。在animationDidStop 委托方法中,我尝试使用CABasicAnimation 对象的toValue 属性设置最终背景颜色。代码在编辑器中似乎是正确的,但是当我编译项目时,链接器找不到CGColor 类。如果我省略 UIColor(CGColor: animation.toValue as CGColor!) 表达式(例如使用 const UIColor),则代码有效
class ViewController: UIViewController {
@IBAction func colorSelected(sender: UIButton)
{
let animation = CABasicAnimation(keyPath: "backgroundColor")
animation.toValue = sender.backgroundColor.CGColor
animation.delegate = self
self.view.layer.addAnimation(animation, forKey: "fadeAnimation")
}
@objc override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
let animation = anim as CABasicAnimation
self.view.backgroundColor = UIColor(CGColor: animation.toValue as CGColor!)
}
}
更新:使用 Xcode 6 beta 3 代码编译,但在方法 swift_dynamicCastUnknownClassUnconditional 中引发异常,其中 toValue 属性被强制转换为 CGColor。
这种奇怪的行为似乎与某些 Xcode 问题有关。
【问题讨论】:
标签: ios core-animation swift cgcolor