【发布时间】:2016-07-28 22:52:14
【问题描述】:
我收到以下错误
无法将类型“StarButton”的值分配给类型 'CAAnimationDelegate?'
在这个 CABasicAnimation 块的最后一行:
let fillCircle = CABasicAnimation(keyPath: "opacity")
fillCircle.toValue = 0
fillCircle.duration = 0.3
fillCircle.setValue(notFavoriteKey, forKey: starKey)
fillCircle.delegate = self // this is where the error is thrown
self 是一个自定义 UIButton 类。这在以前版本的 Swift 中不是问题……有什么解决方案的建议吗?
更新
这里是 StarButton 类的源文件的可下载链接,以供最佳参考:
https://www.dropbox.com/s/gvc2sky05f4p3au/StarButton.swift?dl=0
【问题讨论】:
-
你确定它符合委托协议吗?
-
UIButton来自 Apple Docs (developer.apple.com/library/ios/documentation/UIKit/Reference/…) 不符合CAAnimationDelegate。您是否明确表示您的子类是委托?见stackoverflow.com/questions/24024466/… -
也许 Swift 3 使协议强制转换变得严格了?无论如何,您仍然应该添加协议;如果该类支持成为委托,则应在声明中说明
-
添加一个协议就像添加另一个超类;如果您有类似
class StarButton: UIButton的内容,请替换为class StarButton: UIButton, CABasicAnimationDelegate -
只需将
class StarButton: UIButton {行替换为class StarButton: UIButton, CAAnimationDelegate {。您可能还需要导入 CoreAnimation
标签: ios swift xcode cabasicanimation swift3