监听屏幕亮度

    override func viewDidLoad() {
        super.viewDidLoad()
        UIScreen.main.addObserver(self, forKeyPath: "brightness", options: .new, context: nil)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        print(object ?? "object","---",change ?? "change","---",context ?? "context")   
    }
    
    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        UIScreen.main.brightness  -= 0.1
    }

[__C.NSKeyValueChangeKey(_rawValue: new): 0.4492022693157196,为新的亮度值

> --- [__C.NSKeyValueChangeKey(_rawValue: new): 0.4492022693157196, __C.NSKeyValueChangeKey(_rawValue: kind): 1] --- context
> --- [__C.NSKeyValueChangeKey(_rawValue: new): 0.3492022752761841, __C.NSKeyValueChangeKey(_rawValue: kind): 1] --- context

你可能感兴趣的:(监听屏幕亮度)