zoukankan      html  css  js  c++  java
  • 距离传感器

    // 近距离传感器的实现封装在UIKit中
    
    // 需要使用真机测试
    
    import UIKit
    
    enum YSSession:Int {
        case audio = 1 // 音频
        case video = 2 // 视频
    }
    
    class ViewController: UIViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // 系统APP自动开启了近距离检测(如:打电话,离脸近屏幕就黑了,离的远屏幕主亮了)
            // 但开发者APP需要手动开启
            UIDevice.current.isProximityMonitoringEnabled = true
            
            // 使用通知监听距离变化
            NotificationCenter.default.addObserver(self, selector: #selector(proximityStateChanged), name: NSNotification.Name.UIDeviceProximityStateDidChange, object: nil)
            
            
        /*
            // 使用场景:音视频会话
            let sessionType = 0
            switch sessionType {
            case YSSession.audio.rawValue: // 音频会话,打开近距离传感器
                UIDevice.current.isProximityMonitoringEnabled = true
            case YSSession.video.rawValue: // 视频会话,关闭近距离传感器
                UIDevice.current.isProximityMonitoringEnabled = false
                // 不锁屏
                UIApplication.shared.isIdleTimerDisabled = true
            default:
                break
            }
        */
        }
        
        @objc private func proximityStateChanged(){
            if UIDevice.current.proximityState == true{ // 近距离
                print("太近了,都贴脸上了")
                // 近距离锁屏,就是让屏幕变黑,省电
                UIApplication.shared.isIdleTimerDisabled = false
            } else{ // 远距离
                print("太远了,都看不见你了")
                // 远距离不锁屏
                UIApplication.shared.isIdleTimerDisabled = true
            }
        }
    }
  • 相关阅读:
    数据库chapter 4 数据库安全性
    数据库 chapter 2 关系数据库
    数据库 chapter 1 概论
    操作系统 chapter 11 I/O系统
    操作系统 chapter 12 死锁
    操作系统 chapter 7 8 存储模型
    聊一聊移动调试那些事儿
    获取当前日期和农历的js代码
    使用 CSS 媒体查询创建响应式网站
    大前端工具集
  • 原文地址:https://www.cnblogs.com/panda1024/p/6391482.html
Copyright © 2011-2022 走看看