2017-07-25 30 views
1

我已經找到了答案,以顯示電池百分比 該守則繼是否有返回的任何功能「的時間,直到設備的電池充滿電」

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. 
}} 

問題是與剩下的時間

在此先感謝

+2

觀察如果'UIDevice.current.batteryState == .charging'那麼你可以通過測量經過一段時間的電池電量估計這一點 - 當你知道電池充電率和剩餘百分比,這是一個簡單的計算問題。請注意,這個估計可能不會很準確。 –

+0

@mag_zbc我是新來的迅速。你可以詳細解釋一下,或者如果可能的話,你可以給我一個演示或源代碼。 –

+0

@mag_zbc,我猜蘋果的估計(例如在Mac電腦上)也是非常不準確的,而且很可能依賴於相同的策略。 – LinusGeffarth

回答

0

你可以嘗試測量經過一段時間的電池電量和計算這個自己

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 
} 

注意,這樣的估計也不會很準確。

+0

你可以解釋我「batteryAfterInterval」的用法是什麼, –

2

對此沒有內置功能。你所能做的只是檢查batteryState,當它是charging時,測量UIDeviceBatteryLevelDidChange通知之間的時間以計算充電速度,並根據估計的剩餘充電時間來計算。

但是,由於充電時間與電池百分比不成正比,因此這種方法相當不準確。

+0

這將是很好,如果你能爲我提供某種演示代碼或完整的代碼,因爲我是這個新手 –

0

我還沒有試過,但get clue from this question/answer and convert to Swift.

您可以在ViewWillAppear/ViewDidLoad使用下面NSNotificationCenter

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

每次cad將在代碼下面調用時,都會通知您。

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 
} 

此用戶對您的情況可能有幫助。

而且是不要忘記刪除NSNotificationCenter"viewWillDisAppear"

相關問題