下面是代码使用KVO在Swift中观察view.center为ios返回NSPoint?
view.addObserver(self, forKeyPath: "center", options: .New, context: nil)
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
guard let change = change else {
return
}
print(change)
guard let newCenter = change["new"] else {
return
}
print(newCenter.dynamicType)
}
,输出是:
["new": NSPoint: {50, 50.5}, "kind": 1]
NSConcreteValue
我不知道为什么NS类将出现在iOS版。如何正确观察view.center/frame/transform使用KVO与swift?
“CGRectValue”和“CGSizeValue”是否也存在? –
是的,他们这样做。检查[NSValue类参考](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/index.html#//apple_ref/occ/instm/NSValue/CGSizeValue) 。 –