-
-
Save Ilesh/75fdce3071c0bd4be7641a20ebef68a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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