发布于 2015-12-28 17:12:23
使用函数setTorchModeOnWithLevel设置级别,您可以使用计时器来设置级别更改的时间。
下面是一个如何做到这一点的示例(在全新的AppDelegate单视图应用程序的iOS中实现):
var timer: NSTimer?
func applicationDidBecomeActive(application: UIApplication) {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "delayedAction", userInfo: nil, repeats: true)
}
let maxLightLevel = 5
var lightLevel = 5
func delayedAction() {
guard let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
else {
timer?.invalidate()
return
}
if let _ = try? device.lockForConfiguration() {
defer { device.unlockForConfiguration() }
if lightLevel == 0 {
timer?.invalidate()
device.torchMode = .Off
}
else {
try! device.setTorchModeOnWithLevel(Float(lightLevel)/Float(maxLightLevel))
}
--lightLevel
}
}这段代码只是一个粗略的例子,应该添加适当的行为来处理切换应用程序、抛出的异常等等。
https://stackoverflow.com/questions/34497357
复制相似问题