代码之家  ›  专栏  ›  技术社区  ›  Abhirajsinh Thakore

是否有返回“设备电池充满电之前的时间”的功能

  •  0
  • Abhirajsinh Thakore  · 技术社区  · 8 年前

    代码如下

    class ViewController: UIViewController {
    
    @IBOutlet var infoLabel: UILabel!
    
    var batteryLevel: Float {
        return UIDevice.current.batteryLevel
    }
    var timer = Timer()
    
    func scheduledTimerWithTimeInterval(){
        timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(self.someFunction), userInfo: nil, repeats: true)
    }
    func someFunction() {
    
        self.infoLabel.text = String(format: "%.0f%%", batteryLevel * 100)
            }
    override func viewDidLoad() {
        super.viewDidLoad()
        UIDevice.current.isBatteryMonitoringEnabled = true
        someFunction()
        scheduledTimerWithTimeInterval()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }}
    

    问题在于剩下的时间

    3 回复  |  直到 8 年前
        1
  •  2
  •   Dávid Pásztor    8 年前

    UIDevice 没有用于此的内置功能。你所能做的就是检查 batteryState 当它是 charging ,测量之间的时间 UIDeviceBatteryLevelDidChange 通知以计算充电速度并由此估计剩余充电时间。

        2
  •  1
  •   Govaadiyo    8 年前

    get clue from this question/answer and convert to Swift.

    你可以在下面使用 NSNotificationCenter 在你的 ViewWillAppear/ViewDidLoad

    NotificationCenter.default.addObserver(self, selector: #selector(self.batteryCharged), name: UIDeviceBatteryLevelDidChangeNotification, object: nil)
    

    func batteryCharged() {
        let currBatteryLev: Float = UIDevice.currentDevice.batteryLevel
            // calculate speed of chargement
        let avgChgSpeed: Float = (prevBatteryLev - currBatteryLev) / startDate.timeIntervalSinceNow
            // get how much the battery needs to be charged yet
        let remBatteryLev: Float = 1.0 - currBatteryLev
            // divide the two to obtain the remaining charge time
        let remSeconds: TimeInterval = remBatteryLev / avgChgSpeed
        // convert/format `remSeconds' as appropriate
    }
    

    用户这可能对您的情况有所帮助。

    通知中间 在中观察 "viewWillDisAppear"

        3
  •  0
  •   mag_zbc    8 年前

    你可以试着测量一段时间内的电池电量,然后自己计算

    var batteryLevel : Float = 0.0
    var timer = Timer()
    
    override func viewDidAppear(_ animated: Bool) {
        if UIDevice.current.isBatteryMonitoringEnabled && UIDevice.current.batteryState == .charging {
            batteryLevel = UIDevice.current.batteryLevel
            // measure again in 1 minute
            timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(self.measureAgain), userInfo: nil, repeats: false)
        }
    }
    
    func measureAgain() {
        let batteryAfterInterval = UIDevice.current.batteryLevel
    
        // calculate the difference in battery levels over 1 minute
        let difference = batteryAfterInterval - self.batteryLevel
    
        let remainingPercentage = 100.0 - batteryAfterInterval
    
        let remainingTimeInMinutes = remainingPercentage / difference
    }