我正在尝试将一些适用于 Objective-C 的代码转换为 Swift。我遇到的问题是 needsDisplayForKey/actionForKey 没有以相同的方式被调用。据我所知,自定义键值没有正确传递。这是我调试时得到的结果:
默认值:
(String!) event = {
core = {
_baseAddress = Builtin.RawPointer = 0x00feee51 "onOrderIn"
_countAndFlags = 1073741833
_owner = Some {
Some = (instance_type = Builtin.RawPointer = 0x01026348 @"onOrderIn")
}
}
}
自定义值(传入空字符串):
(String!) event = {
core = {
_baseAddress = Builtin.RawPointer = 0x0b418f79
_countAndFlags = 1073741833
_owner = Some {
Some = (instance_type = Builtin.RawPointer = 0x0b418f70 -> 0x006e38f0 (void *)0x006e38c8: __NSCFString)
}
}
}
我不确定相关代码可能是什么。我只想问 - 还有其他人能够在 Swift 中定义自定义隐式动画吗?从 Objective C 迁移时有什么需要注意的吗?
override class func needsDisplayForKey(key: String!) -> Bool{
if key == "angleFrom" || key == "angleTo" {
return true;
}
return super.needsDisplayForKey(key)
}
override func actionForKey(event: String!) -> CAAction!{
if event == "angleFrom" || event == "angleTo" {
return self.makeAnimationForKey(event)
}
return super.actionForKey(event)
}
最佳答案
我通过在变量声明前使用@NSManaged 属性(您将在 Objective-C 中使用@dynamic 属性)在 Swift 中实现此功能
关于calayer - needsDisplayForKey/actionForKey 覆盖是否正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24150243/