swift5距离传感器
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.isProximityMonitoringEnabled = true
NotificationCenter.default.addObserver(self, selector: #selector(stateChnage), name: UIDevice.proximityStateDidChangeNotification, object: nil)
}
@objc func stateChnage(){
if UIDevice.current.isProximityMonitoringEnabled {
debugPrint("有帅哥靠近")
}else{
debugPrint("有帅哥离开")
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
网友评论