我有以下代码来打开和关闭手电筒:
func toggleTorch(on: Bool) {
guard let device = AVCaptureDevice.default(for: AVMediaType.video)
else {return}
if device.hasTorch {
do {
try device.lockForConfiguration()
if on == true {
device.torchMode = .on
} else {
device.torchMode = .off
}
device.unlockForConfiguration()
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
}我想让它一直闪烁直到我按下按钮。我怎么才能做到呢?
发布于 2018-10-02 20:01:01
你可以试试
var btnNotPressed = true
func blink() {
if device.hasTorch {
do {
try device.lockForConfiguration()
if on == true {
device.torchMode = .on
} else {
device.torchMode = .off
}
// toggle on
on = !on
device.unlockForConfiguration()
// delay until flash shows/hides you can make it 0.5 if you it more fast
DispatchQueue.main.asyncAfter(deadline:.now() + 1 ) {
if btnNotPressed {
self.blink()
}
}
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
}然后在按下按钮时设置btnNotPressed
https://stackoverflow.com/questions/52607642
复制相似问题