バッテリーの残量を取得する
Swift3.0
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myDevice: UIDevice = UIDevice.current
myDevice.isBatteryMonitoringEnabled = true
let myBatteryLevel = myDevice.batteryLevel
let myBatteryState = myDevice.batteryState
print("\(myBatteryLevel) %")
switch (myBatteryState) {
case .full:
self.view.backgroundColor = UIColor.cyan
case .unplugged:
self.view.backgroundColor = UIColor.red
case .charging:
self.view.backgroundColor = UIColor.blue
case .unknown:
self.view.backgroundColor = UIColor.gray
}
let myLabel = UILabel(frame: CGRect(x:0,y:0,width:200,height:200))
myLabel.backgroundColor = UIColor.orange
myLabel.layer.masksToBounds = true
myLabel.layer.cornerRadius = 100.0
myLabel.text = "\(myBatteryLevel * 100) %"
myLabel.textColor = UIColor.white
myLabel.shadowColor = UIColor.gray
myLabel.textAlignment = NSTextAlignment.center
myLabel.layer.position = self.view.center
self.view.addSubview(myLabel)
}
}
Swift 2.3
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myDevice: UIDevice = UIDevice.currentDevice()
myDevice.batteryMonitoringEnabled = true
let myBatteryLevel = myDevice.batteryLevel
let myBatteryState = myDevice.batteryState
print("\(myBatteryLevel) %")
switch (myBatteryState) {
case .Full:
self.view.backgroundColor = UIColor.cyanColor()
case .Unplugged:
self.view.backgroundColor = UIColor.redColor()
case .Charging:
self.view.backgroundColor = UIColor.blueColor()
case .Unknown:
self.view.backgroundColor = UIColor.grayColor()
}
let myLabel = UILabel(frame: CGRectMake(0,0,200,200))
myLabel.backgroundColor = UIColor.orangeColor()
myLabel.layer.masksToBounds = true
myLabel.layer.cornerRadius = 100.0
myLabel.text = "\(myBatteryLevel * 100) %"
myLabel.textColor = UIColor.whiteColor()
myLabel.shadowColor = UIColor.grayColor()
myLabel.textAlignment = NSTextAlignment.Center
myLabel.layer.position = self.view.center
self.view.addSubview(myLabel)
}
}
2.3と3.0の差分
- UIColorの参照方法が変更(UIColor.grayColor()->UIColor.gray)
- CGRect,CGPointの初期化方法の変更(CGRectMake,CGPointMakeの廃止)
- UIDeviceの各メソッド、プロパティ名が変更(
currentDevice()
->current
)
Reference