下面是代碼使用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) 。 –