Skip to content

Instantly share code, notes, and snippets.

@NikhilManapure
Last active January 23, 2019 12:58
Show Gist options
  • Save NikhilManapure/778f0b37b9b3219904bfda3bd71b6df8 to your computer and use it in GitHub Desktop.
Save NikhilManapure/778f0b37b9b3219904bfda3bd71b6df8 to your computer and use it in GitHub Desktop.
// MARK: - KVO
var observedPaths: [String] = []
// Usage - observeKVO(keyPath: #keyPath(camera.inputCamera.whiteBalanceMode))
func observeKVO(keyPath: String) -> Bool {
if shouldObserveKVO {
if self.classForCoder.automaticallyNotifiesObservers(forKey: keyPath) {
observedPaths.append(keyPath)
addObserver(self, forKeyPath: keyPath, options: [.old, .new], context: nil)
return true
}
}
return false
}
func unObserveKVO(keyPath: String) {
if let index = observedPaths.index(of: keyPath) {
observedPaths.remove(at: index)
}
removeObserver(self, forKeyPath: keyPath)
}
func unObserveAllKVO() {
for keyPath in observedPaths {
removeObserver(self, forKeyPath: keyPath)
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let keyPath = keyPath {
switch keyPath {
case #keyPath(camera.inputCamera.iso):
slider.value = camera.inputCamera.iso
default:
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment