代碼基于:swift3.0
題外話:雖然之前對swift有所了解,但是,長時(shí)間不敲swift代碼,生疏的非???。告誡自己,不停的敲敲敲,嗯。
新手總結(jié)
這里只實(shí)現(xiàn)了很基本的東西,主要是熟悉swift代碼
- 開關(guān)手電筒
//控制開、關(guān)
@IBAction func actionTorch(_ sender: UIButton) {
print("torch click")
sender.isSelected = !sender.isSelected
if sender.isSelected {
openTorch(lightLevel: lightLevel)
}else {
closeTorch()
}
}
//開
func openTorch(lightLevel: Float) -> Void {
guard let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) else {
print("no AVCaptureDevice")
return
}
guard ((try? device.lockForConfiguration()) != nil) else {
print("deviece.lockForConfiguration error")
return
}
if device.hasTorch {
guard ((try? device.setTorchModeOnWithLevel(lightLevel)) != nil) else {
print("device.setTorchModeOnWithLevel(lightLevel)")
return
}
}
device.unlockForConfiguration()
}
//關(guān)
func closeTorch() -> Void {
guard let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) else {
print("no device")
return
}
guard ((try? device.lockForConfiguration()) != nil) else {
print("deviece.lockForConfiguration error")
return
}
if device.hasTorch {
device.torchMode = .off
}
device.unlockForConfiguration()
}
- 閃爍
@IBAction func actionFlash(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
//flash
torchBtn.isSelected = true
flashTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.flashTorch), userInfo:nil, repeats: true)
}else {
//end flash
flashTimer.invalidate()
torchBtn.isSelected = false
closeTorch()
ViewController.i = 0
}
}
//怎么閃爍呢,就是定時(shí)器不斷地調(diào),奇數(shù)就開,偶數(shù)就關(guān)
func flashTorch() -> Void {
ViewController.i += 1;
print(ViewController.i)
if ViewController.i%2 == 1 {
openTorch(lightLevel: lightLevel)
}else {
closeTorch()
}
}
- 改變光線
@IBAction func actionChageLight(_ sender: UISlider) {
lightLevel = sender.value
torchBtn.isSelected = true
openTorch(lightLevel: lightLevel)
}
手電筒
后面有時(shí)間,會繼續(xù)更新新的功能
手電筒GitHub地址
遇到的一些問題
- 拖線的時(shí)候報(bào)錯(cuò),“could not insert new action connection could not find any information for the class named”。 如下圖。我的解決方案:重啟xcode

xcode關(guān)掉之后再次打開自己的項(xiàng)目,發(fā)現(xiàn)脫線的方法不執(zhí)行。 我的解決方案:拖線的方法前面變成了空心的圈圈,重新綁定就行了
-
不能聲明靜態(tài)的int變量,報(bào)錯(cuò)"Static properties may only be declared on a type" 。如下圖
在函數(shù)里面定義一個(gè)static的變量會報(bào)錯(cuò).png原因:搜索到一個(gè)回答 Swift supports static variable without having it attached to a class/struct. Try declaring a private struct with static variable. Type properties and methods belong to a type (i.e. a Class, Struct or Enum) and cannot belong to a function alone
解決:放在類里面,不要放在方法里面
放在類里面.png
4.處理異常
guard ((try? device.lockForConfiguration()) != nil) else {
print("deviece.lockForConfiguration error")
return
}
函數(shù)后面帶throws的都要對異常進(jìn)行處理,不處理會報(bào)錯(cuò),swift真的是很安全啊,一點(diǎn)點(diǎn)問題寫錯(cuò),就直接報(bào)錯(cuò),這樣也好,提前做好檢查。我喜歡用這種方式捕獲異常,如果有錯(cuò)誤發(fā)生,方法會返回nil,會進(jìn)入guard大括號里面的方法,沒發(fā)生錯(cuò)誤,就直接往下執(zhí)行啦。
這里隨便給出捕獲異常的幾種方法:處理異常的三種方式
了解就好了,最好用最安全的方式,就是我這種啦。。

