我有以下代码打开和关闭手电筒:
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")
}
}
我要使其闪烁直到按下按钮。我该怎么做?
答案 0 :(得分:0)
您可以尝试
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